简体   繁体   English

Java:如何询问用户是否要继续编程

[英]Java: How to ask user if he/she wants to continue program

I've already made it so that it will tell whether the input from the user is a prime number or not a prime number but now I have to make it so that it will ask the user if he/she wants to check another number and wait for the user to enter "Y" or "y" as yes, "n" or "N" as no. 我已经做过,这样它就可以告诉用户输入的是素数还是不是素数,但是现在我必须这样做,以便它询问用户他/她是否想检查另一个数字,并且等待用户输入“ Y”或“ y”为是,输入“ n”或“ N”为否。 If yes, repeat the three steps. 如果是,请重复三个步骤。 If no, exit the program. 如果否,请退出程序。 For all other letters, reject ti and ask the user to enter only "y" or "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.");
    }
}

This is a pretty standard way I learnt when I started coding. 这是我开始编码时学会的一种非常标准的方法。

Use a do-while loop like this: 使用这样的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')

If the user enter's anything else, the loop will break. 如果用户输入其他内容,则循环将中断。 :) :)

go with switch case 搭配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;
    }
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"));

How about using a while loop 如何使用while循环

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

} }

You Can try this .. 你可以试试这个..

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.");
    }
}

 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); } } } 

  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")); 

I have notice only do while loop not prime no code... 我注意到只做while循环而不是没有代码...

use this code 使用此代码

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();
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:如何询问用户是否想再玩一次 - Java: How to ask the user if he/she wants to play again 如何询问用户他/她是否要退出程序并用Java打印出感谢消息 - How to ask user if he/she wants to quit the program and print out the thank you message in Java 我如何询问用户何时退出程序 - How do I ask the user when he/she wants to quit the program 如何使用 while 循环获取用户输入,询问他/她是要继续还是退出? - How to take user input using while loop asking if he/she wants to continue or exit? 如何问用户是否要重新输入程序(如果键入了字符/字符串而不是数字)? - How to ask user if he wants to start program all over again if he typed a char/string instead of a number? 编写一个 Java 程序,询问用户是否喜欢 Java 编程 - Write a Java program that asks the user if he/she likes Java programming 如何使用 do while 询问用户是否要重复计算 - How to ask the user if he wants to repeat the calculation using the do while 如何询问用户是否要一直显示欢迎页面? - how to ask user if he wants to always show welcome page? Java询问用户是否要继续程序 - Java ask user if they want to continue a program 如果用户输入错误密码超过3次,如何阻止用户在servlet jsp程序中登录 - how to block a user login in servlet jsp program if he/she entered wrong password more than 3 times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM