import java.util.Scanner;
public class whileLoopOlympics
{
public static void main (String[] args)
{
System.out.println ("Which year was the most recent London Olympics?");
Scanner scanner = new Scanner(System.in);
String yearAsString = scanner.nextLine();
int year = Integer.parseInt(yearAsString);
while (year!=2012)
{
System.out.println ("Which year was the most recent London Olympics?");
Scanner next = new Scanner(System.in);
String yearAsString2 = scanner.nextLine();
int year1 = Integer.parseInt(yearAsString2);
}
}
}
我正在尝试循环播放,直到用户输入2012为止,但是我无法弄清楚如何执行诸如while(year!= 2012&& year1!= 2012)之类的操作,而我只是不知道何时执行此操作“年份”不在while循环中
解决方法:
您不需要创建新的扫描程序和新变量,就可以重复使用.
import java.util.Scanner;
public class whileLoopOlympics
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
int year = 0;
while (year!=2012)
{
System.out.println ("Which year was the most recent London Olympics?");
String yearAsString = scanner.nextLine();
year = Integer.parseInt(yearAsString);
}
}
}
标签:java
来源: https://codeday.me/bug/20191118/2030328.html