检查有效整数
Checking for Valid Integer
我目前正在编写一个程序 (java),询问用户他们的 class 时间表,以及每个 class 有多少学分。学分的唯一有效答案应该是 1、2、3 或 4。我如何检查以确保这些是唯一有效的答案?如果他们输入的金额无效,我希望它循环并提示他们提出完全相同的问题。这是我目前所拥有的。
//Jake Petersen
import java.util.Scanner;
public class test{
//I promise not to make all methods static in CS1
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("How many courses are you going to list?");
int courses = Integer.parseInt(scan.nextLine());
String courseArray[] = new String[courses];
for (int i = 0; i < courseArray.length; i++){
System.out.println("Please enter a course:");
courseArray[i] = scan.nextLine();
}
int creditArray[] = new int[courses];
for (int i = 0; i < creditArray.length; i++){
System.out.println("Please enter how many credits " + courseArray[i] + " is:");
creditArray[i] = scan.nextInt();
}
int sum = 0;
for (int i : creditArray){
sum += i;
}
for (int i = 0; i < courseArray.length; i++) {
System.out.print(courseArray[i] + " is a " + creditArray[i] + " credit class. \n");
}
print(sum);
}
public static void print(int sum){
if(sum >= 12 && sum <= 18){
System.out.println("You are taking " + sum + " total credits, which makes you a full time student.");
}else if(sum < 12){
System.out.println("You are taking " + sum + " total credits, which makes you not a full time student.");
}else{
System.out.println("You are taking " + sum + " total credits, which means you are overloaded");
}
}
}
你必须这样做。
Scanner scan = new Scanner(System.in);
boolean a = true;
do{
System.out.println("Insert your Value:");
int value = scan.nextInt();
if(value==1||......)a=false;
}while(a);
您可以将此部分插入到您的代码中。它将再次询问
作为替代方案,您可以简单地将增量从循环中拉出,如果输入有效则只增加 i
,如下所示:
for (int i = 0; i < creditArray.length;) {
System.out.println("Please enter how many credits "+ courseArray[i] + " is:");
int input = scan.nextInt();
if (input >= 1 && input <= 4) {
creditArray[i++] = input;
}
}
您可以使用 while 循环和布尔变量来检查状态
int creditArray[] = new int[courses];
for (int i = 0; i < creditArray.length; i++) {
boolean isValid = true;
while(isValid)
{
System.out.println("Please enter how many credits " + courseArray[i] + " is:");
int buffer = scan.nextInt();
if(buffer == 1|| buffer == 2||buffer == 3||buffer==4)
{
isValid = false;
creditArray[i] = buffer;
}
}
第二种方法是使用像这样的 do-while 语句:
boolean isValid = true;
do{
System.out.println("Please enter how many credits " + courseArray[i] + " is:");
int buffer = scan.nextInt();
if(buffer == 1|| buffer == 2||buffer == 3||buffer==4)
{
a=false;
}
}while(a);
我目前正在编写一个程序 (java),询问用户他们的 class 时间表,以及每个 class 有多少学分。学分的唯一有效答案应该是 1、2、3 或 4。我如何检查以确保这些是唯一有效的答案?如果他们输入的金额无效,我希望它循环并提示他们提出完全相同的问题。这是我目前所拥有的。
//Jake Petersen
import java.util.Scanner;
public class test{
//I promise not to make all methods static in CS1
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("How many courses are you going to list?");
int courses = Integer.parseInt(scan.nextLine());
String courseArray[] = new String[courses];
for (int i = 0; i < courseArray.length; i++){
System.out.println("Please enter a course:");
courseArray[i] = scan.nextLine();
}
int creditArray[] = new int[courses];
for (int i = 0; i < creditArray.length; i++){
System.out.println("Please enter how many credits " + courseArray[i] + " is:");
creditArray[i] = scan.nextInt();
}
int sum = 0;
for (int i : creditArray){
sum += i;
}
for (int i = 0; i < courseArray.length; i++) {
System.out.print(courseArray[i] + " is a " + creditArray[i] + " credit class. \n");
}
print(sum);
}
public static void print(int sum){
if(sum >= 12 && sum <= 18){
System.out.println("You are taking " + sum + " total credits, which makes you a full time student.");
}else if(sum < 12){
System.out.println("You are taking " + sum + " total credits, which makes you not a full time student.");
}else{
System.out.println("You are taking " + sum + " total credits, which means you are overloaded");
}
}
}
你必须这样做。
Scanner scan = new Scanner(System.in);
boolean a = true;
do{
System.out.println("Insert your Value:");
int value = scan.nextInt();
if(value==1||......)a=false;
}while(a);
您可以将此部分插入到您的代码中。它将再次询问
作为替代方案,您可以简单地将增量从循环中拉出,如果输入有效则只增加 i
,如下所示:
for (int i = 0; i < creditArray.length;) {
System.out.println("Please enter how many credits "+ courseArray[i] + " is:");
int input = scan.nextInt();
if (input >= 1 && input <= 4) {
creditArray[i++] = input;
}
}
您可以使用 while 循环和布尔变量来检查状态
int creditArray[] = new int[courses];
for (int i = 0; i < creditArray.length; i++) {
boolean isValid = true;
while(isValid)
{
System.out.println("Please enter how many credits " + courseArray[i] + " is:");
int buffer = scan.nextInt();
if(buffer == 1|| buffer == 2||buffer == 3||buffer==4)
{
isValid = false;
creditArray[i] = buffer;
}
}
第二种方法是使用像这样的 do-while 语句:
boolean isValid = true;
do{
System.out.println("Please enter how many credits " + courseArray[i] + " is:");
int buffer = scan.nextInt();
if(buffer == 1|| buffer == 2||buffer == 3||buffer==4)
{
a=false;
}
}while(a);