-2

我如何返回错误并提出问题你想再试一次(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;
                }
    }
}
4

5 回答 5

3

尝试这样的事情:

while(true){
    System.out.println("Do you want to try again (Y/N)?");
    String input = reader.nextLine();
    if(input.equals("N"))
        break;
    else 
        continue;
}
于 2013-05-03T12:04:27.760 回答
0

你可以在getchoice()方法中添加:

if(!selection.toLowerCase().equals("y") && !selection.toLowerCase().equals("n") ){
    System.out.println("Error");
    return this.getchoice();
}
于 2013-05-03T12:07:30.740 回答
0

首先,将代码重构为方法。然后您可以在程序中的任何位置调用该方法(目前,您调用它两次,因此您可以用这个版本替换两个版本。

然后,添加一个循环...

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方法中。

于 2013-05-03T12:08:06.117 回答
0
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”,那么它也会重复。

于 2013-05-03T12:11:00.383 回答
0
Scanner sc = new Scanner(System.in);
System.out.println("Do you want to run it again (y/n)?: ");
while (!(sc.hasNext() && ((sc.nextLine().equalsIgnoreCase("y"))||
        sc.nextLine().equalsIgnoreCase("n")))){
    System.out.println("Do you want to run it again (y/n)?: ");
    // other code
}
于 2013-05-03T12:06:55.183 回答