2015-10-27 38 views
-1

我目前正在大學裏參加我的第一個編程課,到目前爲止已經非常瞭解如何編寫基本的東西。這個最新的任務讓我感到困惑和困惑。簡單的Java程序,正方形和立方體的用戶輸入編號

我的任務是:

  1. 接受來自用戶(整數)數字輸入
  2. 打印出輸入的數字的平方和立方。
  3. 確保數字大於0.
  4. 重複上述三次。
  5. 如果輸入的數字是< = 0,則結​​束程序,告訴用戶原因。

我的問題是,我不確定變量是如何對於這一點,究竟如何添加循環重複上述過程3次進行設置。

這是我所有迄今爲止,不知道從哪裏去,任何幫助將不勝感激。謝謝

import java.io.*; 

public class Assignment3 //class name here, same as file name 
{ 
    public Assignment3() throws IOException{ //constructor, place class name here 
     // use BufferedReader class to input from the keyboard 
     // declare a variable of type BufferedReader 
     BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 

     //declare variable for input 
     String inputString; 

     // houseKeeping() 
     String yourNumber; 
     int number; 
     int totalSquare = 0; 
     int totalCube = 0; 
     int count; 
     int badNumber=0; 
     String squareCube = " Your number squared is" +square +"your number cubed is"+cube; 


     System.out.print("Enter a number: "); 
     inputString = input.readLine(); 
     yourNumber = inputString; 
    }//end constructor 
} 

public static void main(String [] args) throws IOException 
{ 
    new Assignment3(); //class constructor name 
} 
+0

你沒有使用循環。只需要調用'new Assignment3()'3次 –

+0

如果你想將字符串'yourNumber'轉換爲一個整數,所以你可以將其平方和立方體,使用'Integer.parseInt' http://docs.oracle.com/ javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String) – djhworld

+0

@PaulNikonowicz如果用戶輸入錯誤的數字,你想怎麼做? – Tom

回答

1

我可以張貼的答案在這裏,但那麼這將不會幫助你學習:)

首先,考慮使用Scanner,讓您的輸入。使用BufferedReader將讀取輸入數據爲String。可以使用Integer.parseInt()String轉換爲int,但使用Scanner.nextInt()會更方便。

其次,有關循環:

因爲要循環3次,環以下形式似乎是恰當的:

for (int i=0; i<3; i++) { 
    //read input, calculate square & cube 
} 

在循環中,你要檢查是否有無效項(數字< = 0),如果是,則使用break提前跳出循環並結束程序。

+0

謝謝NickJ的回覆,我將在這個晚上的大部分時間裏工作,並且會在這裏發佈我的最終代碼。我知道我可以做到,我的大腦剛剛還沒有看到它。 – user5495500

+0

看起來和我想發佈的一樣... – St0fF

0

很抱歉的格式錯誤。我試圖太長修復它在網站上...

package com.ispecsoft.porkypig; 

    import java.io.*; 

    public class Assignment3 // class name here, same as file name 
    { 
     public Assignment3() throws Exception 
     { 

     } 

     public static void DoIt() 
     { 

      try 
      { 
       int xIn = 0; 
       BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 

      for (int x = 0; x < 3; x++) 
      {     
       System.out.print("Enter a number: "); 
       xIn = Integer.parseInt(input.readLine()); 

       int iSquare = xIn * xIn; 
       int iCube = iSquare * xIn; 

       System.out.println(x == 0 ? " Your number squared is ("+ iSquare + ") your number cubed is (" + iCube: ") Your number squared is (" + iSquare+ ") your number cubed is (" + iCube + ")"); 
      } 

      } 
      catch (NumberFormatException e) 
      { 
       System.out.print("The character's you entered are not integers.. this application will now close. "); 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      catch (IOException e) 
      { 
       System.out.print("There was an IOException.. this application will now close. "); 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

     public static void main(String[] args) throws Exception 
     { 
     Assignment3.DoIt(); 
     } 
    } 
+0

謝謝,這也是我的想法,我們只是沒有實際瞭解xIn而已 – user5495500

+0

xIn只是一個變量名! – NickJ

+0

如果你喜歡它,你至少可以投票嗎?有一天,當我解決他們的問題時,有人給我投了一票......不確定是誰投了票,但是這並沒有給我很多動力去做這項工作,如果「當我爲某人解決問題時......沒有信貸給予??「 –

相關問題