2015-10-27 39 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

如果你喜欢它,你至少可以投票吗?有一天,当我解决他们的问题时,有人给我投了一票......不确定是谁投了票,但是这并没有给我很多动力去做这项工作,如果“当我为某人解决问题时......没有信贷给予??“ –

相关问题