0
Assigning and displaying at the same time
I have tried to user the scanner to give values to 3 variables. And the Variable value stored using the Scanner. lets see here : Scanner Sc = new Scanner(System.in); System.out.println("Enter the Hour, Minute and Seconds in the Military Format", H=Sc.nextInt(),M=Sc.nextInt(), S=Sc.nextInt() ); This will not be accepted. I have tried Printf, where I can see that there are no errors but nothing happens : Scanner Sc = new Scanner(System.in); System.out.printf("Enter the Hour, Minute and Seconds in the Military Format", H=Sc.nextInt(),M=Sc.nextInt(), S=Sc.nextInt() ); Can any one explain this ?
4 Answers
0
don't use code playground. do it on your computer with an actual Java IDE like eclipse. you can easily see what go wrong.
0
are you doing a separate line for each input in scanner?
0
sorry, my English is bad.
is this code ok...
public static void main(String[] args) {
int H = 0, M = 0, S = 0;
Scanner Sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
switch (i) {
case 0:
H = Sc.nextInt();
break;
case 1:
M = Sc.nextInt();
break;
case 2:
S = Sc.nextInt();
break;
}
}
System.out.println("Enter the Hour, Minute and Seconds in the Military Format" + H + "," + M + "," + S);
}
0
the scanner needs importing and initialized to int i,h,m,s to work propley, what exactly are you trying to achieve as you have a for loop in there as well?
get back to me and ill fix your code.