2013-10-01 24 views
-5

我已經制作了它,以便它能夠判斷用戶的輸入是素數還是不是素數,但現在我必須讓它因此它會詢問用戶他/她是否想檢查另一個號碼並等待用戶輸入「Y」或「Y」爲「是」,「否」或「否」爲否。如果是,請重複這三個步驟。如果否,退出程序。對於所有其他字母,拒絕ti並要求用戶只輸入「y」或「n」。Java:如何詢問用戶他/她是否想要繼續編程

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

如果你能夠編寫了大家展示一下,它的其餘部分應該是很容易的,你試過嗎? –

+5

首先看看[do-while](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html)循環 – MadProgrammer

+0

@MadProgrammer:的確如此。男孩,這把我帶回來。 – user2339071

回答

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; 
    } 
+0

當我將其添加到我的程序 –

+0

時,這不起作用請參閱我更新了我的答案。 –

+0

只能打開整數或整數值 – singe3

0

如何使用while循環

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

}

+1

'equalsIgnoreCase'會更好一點......? – MadProgrammer

2

這是一個非常標準的方式,當我開始編碼我的教訓。

使用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') 

,循環將打破。 :)

+0

'@ user'根據要求,如果用戶輸入其他內容,用戶再次要求輸入(是/否)。 – HybrisFreelance

+0

我猜這個問題的人可以相應地修改代碼。我們在這裏爲這些問題提供提示/想法。目的不是要滿足所有的「要求」。 – user2339071

+0

'@ user'我同意你的觀點,但是如果我們能夠輕鬆地做到這一點,那麼更多的幫助完全可以提出質疑。 – HybrisFreelance

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

1-爲什麼(「使用此代碼」)2-就我個人而言,我認爲一個'do-while'循環會更合乎邏輯,因爲您希望執行第一個循環,只是說;) – MadProgrammer

+1

可能花時間縮進代碼。這樣閱讀要容易得多。 – ChiefTwoPencils

+0

是啊真的,但這只是解決她的問題的另一種方法:) –

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

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

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

 
\t public static void main(String[] args) { 
 

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

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

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

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

 
\t \t \t \t total_sold += quantity; 
 

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

 
\t \t } 
 

 
\t 
 
\t } 
 
}

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循環不是素數無碼...

相關問題