Shouldn't minstetall be the lowest number here? It only prints 0.
import java.util.*; public class Kap3Oppg5 { public static void main(String[] args) { int x = 0; int tabell[] = new int[10]; int heltall = 0; Scanner in = new Scanner(System.in); int minstetall = tabell[0]; int indeks = 0; while (x < 10) { System.out.println("Tast inn neste tall:"); heltall = in.nextInt(); tabell[x] = heltall; x = x + 1; } for (int i=0;i<tabell.length;i++) { if (tabell[i] < tabell[0]) minstetall = tabell[i]; } System.out.print(minstetall); } }
EDIT: I am happy to report that I solved the problem myself. If anyone should have problems with anything that looks the same, here is the code: (Note that the full task was to find the lowest integer in the array, report which index it had, and swap said number with tabell[0].)
import java.util.*; public class Kap3Oppg5 { public static void main(String[] args) { int x = 0; int tabell[] = new int[10]; int heltall; Scanner in = new Scanner(System.in); int minstetall = tabell[0]; // int indeks = 0; while (x < 10) { System.out.println("Tast inn neste tall:"); heltall = in.nextInt(); tabell[x] = heltall; //Definerer heltallet som tabell[x] x = x + 1; //Hvor x begynner på 0 og øker til 9. } for (int i=0;i<tabell.length;i++) { if (tabell[i] < tabell[indeks]) indeks = i; } minstetall = tabell[indeks]; System.out.println("Det minste tallet er " +minstetall); System.out.println("Dette tallet har indeksen " +indeks); int midlertidig = tabell[indeks]; tabell[indeks] = tabell[0]; tabell[0] = midlertidig; System.out.println("Den nye rekkefølgen på tallene er: "); System.out.println(tabell[0]); System.out.println(tabell[1]); System.out.println(tabell[2]); System.out.println(tabell[3]); System.out.println(tabell[4]); System.out.println(tabell[5]); System.out.println(tabell[6]); System.out.println(tabell[7]); System.out.println(tabell[8]); System.out.println(tabell[9]); } }