Java 键入“是/否”时如何返回错误?
如何返回错误并询问您是否要重试(Y/N)?当用户再次输入“是/否”作为答案时Java 键入“是/否”时如何返回错误?,java,Java,如何返回错误并询问您是否要重试(Y/N)?当用户再次输入“是/否”作为答案时 package randomgenerate; /** * * @author Trung */ public class World { public static void main(String[] args) { int max=0; int min=0; double aver = 0; int option; rg
package randomgenerate;
/**
*
* @author Trung
*/
public class World {
public static void main(String[] args) {
int max=0;
int min=0;
double aver = 0;
int option;
rg a = new rg();
a.generate();
a.count();
System.out.println("Max number is "+ a.maximum(max));
System.out.println("Average number is "+ a.average(aver));
System.out.println("Min number is "+ a.minimum(min));
System.out.println("Do you want to run it again (y/n)?: ");
option = a.getchoice();
switch (option)
{
case 1:
{
a.generate();
a.count();
System.out.println("Max number is "+ a.maximum(max));
System.out.println("Average number is "+ a.average(aver));
System.out.println("Min number is "+ a.minimum(min));
System.out.println("Do you want to run it again (y/n)?: ");
option = a.getchoice();
}
case 2:
{
System.out.println("Program exits.");
System.exit(0);
}
case 3:
{
System.out.println("Invalid. Please enter gyh or gnh: ");
a.getchoice();
}
}
}
}
package randomgenerate;
import java.util.Scanner;
/**
*
* @author Trung
*/
public class rg {
//generate 100 random number
int s[]= new int[101];
public void generate (){
int i, random;
System.out.println("Generating 100 random integers between 0 and 9");
for (i=1; i<=100;i++)
{
s[i] = (int)(Math.random()*10+0);
System.out.println("Number "+i+" = "+ s[i]);
}
}
//count
public void count (){
int i;
int count0=0;
int count1=0;
int count2=0;
int count3=0;
int count4=0;
int count5=0;
int count6=0;
int count7=0;
int count8=0;
int count9=0;
for (i=1; i<=100; i++)
{
if (s[i]==0)
{
count0++;
}
else if (s[i]==1)
{
count1++;
}
else if (s[i]==2)
{
count2++;
}
else if (s[i]==3)
{
count3++;
}
else if (s[i]==4)
{
count4++;
}
else if (s[i]==5)
{
count5++;
}
else if (s[i]==6)
{
count6++;
}
else if (s[i]==7)
{
count7++;
}
else if (s[i]==8)
{
count8++;
}
else if (s[i]==9)
{
count9++;
}
}
if (count0 <= 1)
{
System.out.println("0 occurs "+ count0 + " time");
}
else
{
System.out.println("0 occurs "+ count0 + " times");
}
if (count1 <= 1)
{
System.out.println("1 occurs "+ count1 + " time");
}
else
{
System.out.println("1 occurs "+ count1 + " times");
}
if (count2 <= 1)
{
System.out.println("2 occurs "+ count2 + " time");
}
else
{
System.out.println("2 occurs "+ count2 + " times");
}
if (count3 <= 1)
{
System.out.println("3 occurs "+ count3 + " time");
}
else
{
System.out.println("3 occurs "+ count3 + " times");
}
if (count4 <= 1)
{
System.out.println("4 occurs "+ count4 + " time");
}
else
{
System.out.println("4 occurs "+ count4 + " times");
}
if (count5 <= 1)
{
System.out.println("5 occurs "+ count5 + " time");
}
else
{
System.out.println("5 occurs "+ count5 + " times");
}
if (count6 <= 1)
{
System.out.println("6 occurs "+ count6 + " time");
}
else
{
System.out.println("6 occurs "+ count6 + " times");
}
if (count7 <= 1)
{
System.out.println("7 occurs "+ count7 + " time");
}
else
{
System.out.println("7 occurs "+ count7 + " times");
}
if (count8 <= 1)
{
System.out.println("8 occurs "+ count8 + " time");
}
else
{
System.out.println("8 occurs "+ count8 + " times");
}
if (count9 <= 1)
{
System.out.println("9 occurs "+ count9 + " time");
}
else
{
System.out.println("9 occurs "+ count9 + " times");
}
}
public int maximum (int max)
{
max = s[0];
for (int i=1;i<=100;i++)
{
if(max<s[i])
{
max=s[i];
}
}
return (max);
}
public int minimum (int min)
{
min = s[0];
for (int i=1;i<=100;i++)
{
if(s[i]<min)
{
min=s[i];
}
}
return (min);
}
public double average (double aver)
{
int i = 1;
int subtotal1 = 0;
int subtotal2 = 0;
int subtotal3 = 0;
int subtotal4 = 0;
int subtotal5 = 0;
int subtotal6 = 0;
int subtotal7 = 0;
int subtotal8 = 0;
int subtotal9 = 0;
for (i=1;i<=100;i++)
{
if (s[i]==1)
{
subtotal1 = subtotal1 + s[i];
}
else if (s[i]==2)
{
subtotal2 = subtotal2 +s[i];
}
else if (s[i]==3)
{
subtotal3 = subtotal3 +s[i];
}
else if (s[i]==4)
{
subtotal4 = subtotal4 +s[i];
}
else if (s[i]==5)
{
subtotal5 = subtotal5 +s[i];
}
else if (s[i]==6)
{
subtotal6 = subtotal6 +s[i];
}
else if (s[i]==7)
{
subtotal7 = subtotal7 +s[i];
}
else if (s[i]==8)
{
subtotal8 = subtotal8 +s[i];
}
else if (s[i]==9)
{
subtotal9 = subtotal9 +s[i];
}
}
aver = (subtotal1 + subtotal2 + subtotal3 + subtotal4 + subtotal5 + subtotal6 + subtotal7 + subtotal8 + subtotal9) * 0.01;
return (aver);
}
public int getchoice() {
Scanner reader = new Scanner(System.in);
String selection = reader.nextLine();
if (selection.equals("y") || selection.equals("Y")){
return 1;
}
else if (selection.equals("n") || selection.equals("N")){
return 2;
}
else
{
return 3;
}
}
}
包随机生成;
/**
*
*@author Trung
*/
公共阶级世界{
公共静态void main(字符串[]args){
int max=0;
int min=0;
双平均=0;
int选项;
rga=新rg();
a、 生成();
a、 计数();
System.out.println(“最大数量为”+a.Max(最大));
System.out.println(“平均数为”+a.Average(aver));
系统输出打印项次(“最小值为”+a.最小值(Min));
System.out.println(“是否要再次运行它(y/n)?:”;
option=a.getchoice();
开关(选件)
{
案例1:
{
a、 生成();
a、 计数();
System.out.println(“最大数量为”+a.Max(最大));
System.out.println(“平均数为”+a.Average(aver));
系统输出打印项次(“最小值为”+a.最小值(Min));
System.out.println(“是否要再次运行它(y/n)?:”;
option=a.getchoice();
}
案例2:
{
System.out.println(“程序退出”);
系统出口(0);
}
案例3:
{
System.out.println(“无效。请输入gyh或gnh:”);
a、 getchoice();
}
}
}
}
包装随机生成;
导入java.util.Scanner;
/**
*
*@author Trung
*/
公共类rg{
//生成100个随机数
int s[]=新int[101];
公共void生成(){
int i,随机;
System.out.println(“生成0到9之间的100个随机整数”);
对于(i=1;i请尝试以下内容:
while(true){
System.out.println("Do you want to try again (Y/N)?");
String input = reader.nextLine();
if(input.equals("N"))
break;
else
continue;
}
您只需添加getchoice()
方法:
if(!selection.toLowerCase().equals("y") && !selection.toLowerCase().equals("n") ){
System.out.println("Error");
return this.getchoice();
}
首先,将代码重构成一个方法,然后你可以在你程序中的任何地方调用这个方法(目前,你调用它两次,所以你可以用这个版本替换两个版本)
然后,添加一个循环
public static int getRunAgain(rg a) {
int option = 3;
System.out.println("Max number is "+ a.maximum(max));
System.out.println("Average number is "+ a.average(aver));
System.out.println("Min number is "+ a.minimum(min));
while(true) {
System.out.println("Do you want to run it again (y/n)?: ");
option = a.getchoice();
if (option != 3) break;
System.out.println("Invalid. Please enter y/n. ");
}
return option;
}
另一种方法是将循环放入getChoice
方法中
do
{
System.out.println("Max number is "+ a.maximum(max));
System.out.println("Average number is "+ a.average(aver));
System.out.println("Min number is "+ a.minimum(min));
System.out.println("Do you want to run it again (y/n)?: ");
option = a.getchoice();
}while(option.equals("Y") || !option.equals("N"));
如果用户输入Y,它将重复。同样,如果用户既不输入“Y”也不输入“N”,然后它也会重复。请只包括代码的重要部分。没有人会调查数百行。请尽量缩短代码,以便问题中只包含相关部分,如果与问题无关,我们不需要整个程序。同时,将问题键入两次也没有意义
do
{
System.out.println("Max number is "+ a.maximum(max));
System.out.println("Average number is "+ a.average(aver));
System.out.println("Min number is "+ a.minimum(min));
System.out.println("Do you want to run it again (y/n)?: ");
option = a.getchoice();
}while(option.equals("Y") || !option.equals("N"));