我目前正在大学里参加我的第一个编程课,到目前为止已经非常了解如何编写基本的东西。这个最新的任务让我感到困惑和困惑。简单的Java程序,正方形和立方体的用户输入编号
我的任务是:
- 接受来自用户(整数)数字输入
- 打印出输入的数字的平方和立方。
- 确保数字大于0.
- 重复上述三次。
- 如果输入的数字是< = 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
}
你没有使用循环。只需要调用'new Assignment3()'3次 –
如果你想将字符串'yourNumber'转换为一个整数,所以你可以将其平方和立方体,使用'Integer.parseInt' http://docs.oracle.com/ javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String) – djhworld
@PaulNikonowicz如果用户输入错误的数字,你想怎么做? – Tom