-3

我已经做到了,这样它就可以判断用户的输入是否是素数,但现在我必须这样做,以便它会询问用户他/她是否想检查另一个数字和等待用户输入“Y”或“y”为是,“n”或“N”为否。如果是,重复三个步骤。如果不是,退出程序。对于所有其他字母,拒绝 ti 并要求用户仅输入“y”或“n”。

import java.util.*;  // importing package util


public class Prime
{
    public static void main (String args[])
    {
        int num,count=0;
        Scanner scan = new Scanner(System.in); //scanner for input
        System.out.print("Enter any number : ");
        num = scan.nextInt();
        for(int i=2; i <= (num/2); i++) 
        {
            if((num % i) == 0)
            {
                count++;
                break;
            }
        }
        if((count==0) && (num!= 1))
        System.out.println( num + " is a prime number.");
        else
        System.out.println( num + " is not a prime number.");
    }
}
4

9 回答 9

3

这是我开始编码时学习的一种非常标准的方式。

使用这样的do-while循环:

do
{
 System.out.print("Enter any number : ");
        num = scan.nextInt();
        for(int i=2; i <= (num/2); i++) 
        {
            if((num % i) == 0)
            {
                count++;
                break;
            }
        }
        if((count==0) && (num!= 1))
        System.out.println( num + " is a prime number.");
        else
        System.out.println( num + " is not a prime number.");
        System.out.println("Continue(Y/N)");
        char a = scan.next();

} while(a=='Y'|| a=='y')

如果用户输入其他内容,则循环将中断。:)

于 2013-10-01T05:57:12.010 回答
2
String awnser="";
do{

        int num,count=0;
        Scanner scan = new Scanner(System.in); //scanner for input
        System.out.print("Enter any number : ");
        num = scan.nextInt();
        for(int i=2; i <= (num/2); i++) 
        {
            if((num % i) == 0)
            {
                count++;
                break;
            }
        }
        if((count==0) && (num!= 1))
        System.out.println( num + " is a prime number.");
        else
        System.out.println( num + " is not a prime number.");

        System.out.println("do you want to continue?");
        awnser=scan.next();

}while(awnser.equals("y"));
于 2013-10-01T05:57:39.003 回答
1

一起去switch case

System.out.print("DO you to continue Y/N: ");
    String answer = scan.nextLine();


    switch (answer)
    {
    case "Y"
    case "y": //WRITE YOUR CODE HERE
             break;
    case "N"
    case "n"://exit from program;
             break;

    default :
             System.out.println("invalid choice")   ;
             break;
    }
于 2013-10-01T05:51:18.953 回答
0

import java.io.InputStream;
import java.util.Scanner;

public class example1 {
	static int value;
	static int quantity;
	static int total = 0;
	static int total_sold = 0;
	static int total_cashier = 0;
	static float expensier = 0;
	int again = 1;
	static String answer;

	public static void main(String[] args) {

		while (true) {
			System.out.print("Are You Want to Continue (Y/N) ?\n");
			Scanner ans = new Scanner(System.in);
			answer =ans.nextLine();
			if (answer.equalsIgnoreCase("Y")) {
				Scanner input = new Scanner(System.in);
				System.out.println("Input Price: \n");
				value = input.nextInt();

				Scanner input2 = new Scanner(System.in);
				System.out.println("Input Quantity: \n");
				quantity = input2.nextInt();

				total += value * quantity;
				total_cashier += total;

				if (value > expensier) {
					expensier = value;
				}

				total_sold += quantity;

			} else if (answer.equalsIgnoreCase("N")) {
				System.out.print("Thank you !!");
				break;
			} else {
				System.out.print("Try Again With (Y/N) only !");
			}
			System.out.println("Total to pay :" + total);
			System.out.println("Value of the expensiest product: " + expensier);
			System.out.println("Amount of products sold: " + total_sold);
			System.out.println("Amount of cash in cashier: " + total_cashier);

		}

	
	}
}

于 2015-04-28T13:02:17.587 回答
0

使用while循环怎么样

while (!"N".equals(inputValues) {
//Keep asking for user inputs

}

于 2013-10-01T05:52:40.060 回答
0

你可以试试这个..

import java.util.Scanner;

// importing package util

public class Prime
{
    static Scanner scan = new Scanner(System.in);
    static String userInput = null;

    public static void main(String args[])
    {
        checkPrime();
        while (true)
        {
            System.out.print("Are You Want to Continue (Y/N) ?");
            userInput = scan.next();
            if (userInput.equalsIgnoreCase("Y"))
                checkPrime();
            else if (userInput.equalsIgnoreCase("N"))
            {
                System.out.print("Thank you !!");
                break;
            }
            else{
                            System.out.print("Try Again With (Y/N) only !");   
            }

        }

    }

    public static void checkPrime()
    {
        int num, count = 0;
        System.out.print("Enter any number : ");
        num = scan.nextInt();
        for (int i = 2; i <= (num / 2); i++)
        {
            if ((num % i) == 0)
            {
                count++;
                break;
            }
        }
        if ((count == 0) && (num != 1))
            System.out.println(num + " is a prime number.");
        else
            System.out.println(num + " is not a prime number.");
    }
}
于 2013-10-01T06:06:17.367 回答
0
                package Loops;
                import java.util.Scanner;
                public class Question11 {
                    public static void main(String[] args) {
                        Scanner sc= new Scanner(System.in);
                        String a="";
                        do {
                            System.out.println("Enter the first number: \n");
                            int x = sc.nextInt();
                            System.out.println("Enter the second number: \n");
                            int y = sc.nextInt();
                            while (x < 0 || y < 0) {
                                System.out.println("Kindly enter the positive numbers: ");
                                x = sc.nextInt();
                                y = sc.nextInt();
                            }
                            int z = x + y;
                            System.out.println("The addition of 2 numbers is: " + z);
                            System.out.println("Do you want to do the addition again: press y or n");
                            a = sc.next();
                        }while (a.equals("y") || a.equals("Y"));
                        System.out.println("Thanks for using the application: ");
                
              
于 2021-08-27T17:15:50.047 回答
0

  private static String reply;
     public static void main(String[] args){
         int num,count=0;
        Scanner scan = new Scanner (System.in);
        do
{
 System.out.print("Enter any number : ");
        num = scan.nextInt();
        for(int i=2; i <= (num/2); i++) 
        {
            if((num % i) == 0)
            {
                count++;
                break;
            }
        }
        if((count==0) && (num!= 1))
        System.out.println( num + " is a prime number.");
        else
        System.out.println( num + " is not a prime number.");
        System.out.println("Continue(Y/N)");
         reply = scan.next().toUpperCase();

        } while(reply.equals("Y"));

我注意到只做while循环而不是prime没有代码......

于 2016-09-22T14:43:02.970 回答
-1

使用此代码

import java.util.*; 

public class stackof
{
public static void main (String args[])
{
    int num,count=0;
    String s="y";
    Scanner scan = new Scanner(System.in); //scanner for input
    System.out.print("Enter any number : ");

    while(s.equalsIgnoreCase("y")){
        num = scan.nextInt();
      for(int i=2; i <= (num/2); i++)
      {
        if((num % i) == 0)
        {
            count++;
            break;
        }
      }

      if((count==0) && (num!= 1))
         System.out.println( num + " is a prime number.");
      else
         System.out.println( num + " is not a prime number.");

      System.out.println( "Do you want to enter another no?");
      s=scan.next();
    }
  }
}
于 2013-10-01T06:00:25.063 回答