2015-10-22 81 views
-1

所以只是想確保我的代碼沒問題。要求用戶輸入一個數字

其目標是顯示一個由兩個不同數字組成的高四行三角形。該程序將要求兩個數字:一個在0和4之間,一個在5和9之間。三角形的第一和第三行將填充第一個數字;三角形的第二行和最後一行將填入第二個數字。

這裏是我的代碼:

/*This program displays a triangle 4 lines high, made up of two different 
 
numbers. The program will ask for two numbers: one between 0 and 4 
 
and one between 5 and 9. The first and third line of the triangle 
 
will be filled with the first number; the second and last line of the 
 
triangle will be filled with the second number. */ 
 
import java.util.Scanner; 
 

 
public class Question { 
 
    public static void main(String args[]) { 
 
    Scanner keyboard = new Scanner (System.in); 
 
     int num1, num2; 
 
     
 
\t System.out.print("Input a number between 0-4: "); 
 
     num1 = keyboard.nextInt(); 
 
     
 
     System.out.print("Input a number between 5-9: "); 
 
     num2 = keyboard.nextInt(); 
 
     
 
     if (num1>=0 && num1<=4 && num2>=5 && num2<=9){ 
 
     System.out.println(" \t " + num1); 
 
     System.out.println(" \t "+num2 +" "+ num2); 
 
     System.out.println(" \t " + num1+" "+ num1+" "+ num1); 
 
     System.out.println("\t"+num2 +" "+ num2+" "+ num2+" "+ num2); 
 

 
     } 
 
     else{ 
 
      
 
     System.out.println("Please Try again!"); 
 
     } 
 
    } 
 
}

+2

我不能說出你的問題是什麼......你的代碼有什麼問題嗎?或者您遇到的特定問題?它是否按預期執行? 編輯:我試過了,它完全符合你所描述的...我看不到任何問題? – SJB

回答

1

您的代碼看起來好像沒什麼問題。我不知道你的問題是什麼,但我做了一些測試:

[email protected]:~$ javac Question.java 
[email protected]:~$ java Question 
Input a number between 0-4: 4 
Input a number between 5-9: 8 
     4 
     8 8 
    4 4 4 
    8 8 8 8 
[email protected]:~$ java Question 
Input a number between 0-4: 0 
Input a number between 5-9: 2 
Please Try again! 
[email protected]:~$ java Question 
Input a number between 0-4: 4 
Input a number between 5-9: 2 
Please Try again! 
[email protected]:~$ java Question 
Input a number between 0-4: 6 
Input a number between 5-9: 9 
Please Try again! 
[email protected]:~$ java Question 
Input a number between 0-4: 2 
Input a number between 5-9: 4 
Please Try again! 
[email protected]:~$ 

在這裏一切看起來不錯!

相關問題