我试图让程序完成: 如果未接受输入的数据,请再次请求该信息 (注意:可以再次请求所有信息,没有必要只要求重新输入特定信息,但如果您愿意,可以。)
对于程序,一切似乎运行正常,除了它的分辨率,它要求另一个输入,但如果输入不正确它只是接受。我需要它继续运行,直到输入正确的输入。
import java.util.Scanner;
public class encap
{
private static String userID;
private static String password;
private static String resolution;
private static int Ramsize;
private static int freespace;
private static int videocard;
//Get and Set methods
public static String getuserID()
{
Scanner input = new Scanner(System.in);
System.out.print("Please enter userID : ");
userID = input.next();
return userID;
}
public static String getpassword()
{
Scanner input = new Scanner(System.in);
System.out.print("Please enter password : ");
password = input.next();
return password;
}
public static String getresolution()
{
Scanner input = new Scanner(System.in);
System.out.print("Please enter video resolution: ");
resolution = input.next();
if (resolution.equals("800x600") || resolution.equals("1024x768") || resolution.equals("1152x900"));
else
{
while(true)
{
System.out.println("Information invalid, Please fill again");
String getresolution = input.next();
if (resolution.equals("800x600") || resolution.equals("1024x768") || resolution.equals("1152x900"));
break;
}
}
return resolution;
}
public static int getRamsize()
{
Scanner input = new Scanner(System.in);
System.out.print("Please enter RAM size : ");
while(true)
{
if(input.hasNextInt())
{
Ramsize = input.nextInt();
break;
}
else
{
input.nextLine();
System.out.println("Invalid Input! Integer required");
System.out.print("Please enter RAM size : ");
}
}
return Ramsize;
}
public static int getfreespace()
{
Scanner input = new Scanner(System.in);
System.out.print("Please enter HD free space : ");
while(true)
{
if(input.hasNextInt())
{
freespace = input.nextInt();
break;
}
else
{
input.nextLine();
System.out.println("Invalid Input! Integer required");
System.out.print("Please enter HD free space : ");
}
}
return freespace;
}
public static int getvideocard()
{
Scanner input = new Scanner(System.in);
System.out.print("Please enter video card RAM size: ");
while(true)
{
if(input.hasNextInt())
{
videocard = input.nextInt();
break;
}
else
{
input.nextLine();
System.out.println("Invalid Input! Integer required");
System.out.print("Please enter video card RAM size: ");
}
}
return videocard;
}
public static void setuserID(String newuserID)
{
userID = newuserID;
}
public static void setpassword(String newpassword)
{
password = newpassword;
}
public static void setresolution(String newresolution)
{
resolution = newresolution;
}
public static void setRamsize (int newRamsize)
{
Ramsize = newRamsize;
}
public static void setfreespace (int newfreespace)
{
freespace = newfreespace;
}
public static void setvideocard (int newvideocard)
{
videocard = newvideocard;
}
public static void main(String[] args)
{
setuserID(getuserID());
setpassword(getpassword());
setresolution(getresolution());
setRamsize(getRamsize());
setfreespace(getfreespace());
setvideocard(getvideocard());
System.out.println("You have input the following information: " + "\nuserID: " + userID
+ "\npassword: " + password + "\nVideo resolution: " + resolution + "\nRam Size: "
+ Ramsize + "\nHD Free Space: " + freespace + "\nVideo Card Ram Size: " + videocard);
}
}
答案 0 :(得分:0)
问题是因为你在有效案例场景中从不做任何事情而你正在使用.next()代替.nextLine(),它会抓住在行尾字符后面输入的整个输入(返回字符)
这将询问,直到输入的输入满足您的if条件。
NULL