Java 用于循环和封装
我正在尝试让程序执行以下操作: 如果输入的数据不被接受,请再次请求信息 (注意:可以再次请求所有信息,无需仅请求重新输入特定信息,但如果愿意,您可以) 对于这个程序来说,除了当它达到分辨率时,它要求另一个输入,但是如果输入不正确,它就接受。我需要它一直运行,直到输入正确Java 用于循环和封装,java,encapsulation,Java,Encapsulation,我正在尝试让程序执行以下操作: 如果输入的数据不被接受,请再次请求信息 (注意:可以再次请求所有信息,无需仅请求重新输入特定信息,但如果愿意,您可以) 对于这个程序来说,除了当它达到分辨率时,它要求另一个输入,但是如果输入不正确,它就接受。我需要它一直运行,直到输入正确 import java.util.Scanner; public class encap { private static String userID; private static String passwo
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);
}
}
问题在于,您从未在有效的case场景中执行任何操作,而是将.next()用于单个字符,而不是.nextLine(),它将获取在行尾字符(返回字符)之后输入的整个输入 这将一直询问,直到输入的输入满足if条件
public static String getresolution()
{
String resolution;
boolean validAnswer = false;
Scanner input = new Scanner(System.in);
HashSet<String> validResolutions = new HashSet<>();
validResolutions.add("800x600");
validResolutions.add("1024x768");
validResolutions.add("1152x900");
//add more resolutions if you want without having to create a bigger if check
//validResolutions.add("1400x1120");
do {
System.out.print("Please enter video resolution: ");
resolution = input.nextLine().replaceAll(" ", "").replaceAll("\n", "");
validAnswer = validResolutions.contains(resolution) ? true : false;
if(!validAnswer)
System.out.println("Incorrect resolution please try again");
} while (!validAnswer);
return resolution;
}
公共静态字符串getresolution()
{
字符串分辨率;
布尔值validAnswer=false;
扫描仪输入=新扫描仪(System.in);
HashSet validResolutions=新HashSet();
validResolutions.添加(“800x600”);
validResolutions.添加(“1024x768”);
validResolutions.添加(“1152x900”);
//如果需要,可以添加更多分辨率,而无需创建更大的if检查
//validResolutions.添加(“1400x1120”);
做{
System.out.print(“请输入视频分辨率:”);
分辨率=input.nextLine().replaceAll(“,”).replaceAll(“\n”,”);
validAnswer=validResolutions.contains(分辨率)?true:false;
如果(!validAnswer)
System.out.println(“分辨率不正确,请重试”);
}而(!validAnswer);
返回分辨率;
}
问题在于,您从未在有效的案例场景中执行任何操作,而是将.next()用于单个字符,而不是.nextLine(),它会获取在行尾字符(返回字符)之后输入的整个输入
这将一直询问,直到输入的输入满足if条件
public static String getresolution()
{
String resolution;
boolean validAnswer = false;
Scanner input = new Scanner(System.in);
HashSet<String> validResolutions = new HashSet<>();
validResolutions.add("800x600");
validResolutions.add("1024x768");
validResolutions.add("1152x900");
//add more resolutions if you want without having to create a bigger if check
//validResolutions.add("1400x1120");
do {
System.out.print("Please enter video resolution: ");
resolution = input.nextLine().replaceAll(" ", "").replaceAll("\n", "");
validAnswer = validResolutions.contains(resolution) ? true : false;
if(!validAnswer)
System.out.println("Incorrect resolution please try again");
} while (!validAnswer);
return resolution;
}
公共静态字符串getresolution()
{
字符串分辨率;
布尔值validAnswer=false;
扫描仪输入=新扫描仪(System.in);
HashSet validResolutions=新HashSet();
validResolutions.添加(“800x600”);
validResolutions.添加(“1024x768”);
validResolutions.添加(“1152x900”);
//如果需要,可以添加更多分辨率,而无需创建更大的if检查
//validResolutions.添加(“1400x1120”);
做{
System.out.print(“请输入视频分辨率:”);
分辨率=input.nextLine().replaceAll(“,”).replaceAll(“\n”,”);
validAnswer=validResolutions.contains(分辨率)?true:false;
如果(!validAnswer)
System.out.println(“分辨率不正确,请重试”);
}而(!validAnswer);
返回分辨率;
}
为什么if-inside getresolution后面有一个分号?如果输入是有效的,您就不会做任何事情。现在循环只会继续运行并请求输入,即使我输入800x600,它也不会中断。if
条件中的分号问题更不用说,但是if
条件中有一个while(true)
条件会导致无限循环;)为什么您的if-inside-getresolution后面有一个分号?如果输入是有效的,您就不会做任何事情。现在循环只会继续运行并请求输入,即使我输入800x600,它也不会中断。if
条件中的分号问题更不用说,但是if
条件中有一个while(true)
条件会导致无限循环;)太好了,如果可以结束问题,请随意标记为答案:)祝你剩余的作业好运。太好了,如果可以结束问题,请随意标记为答案:)祝你剩余的作业好运。