我如何返回错误并提出问题你想再试一次(Y/N)吗?当用户没有输入 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 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;
}
}
}