【发布时间】:2015-10-27 20:38:51
【问题描述】:
我目前正在上大学的第一堂编程课,到目前为止,我已经非常了解如何编写基本的东西。这个最新的任务让我感到困惑和难过。
我的任务是:
- 接受用户的数字输入(整数值)
- 打印出输入数字的正方形和立方体。
- 确保数字大于 0。
- 重复上述操作 3 次。
- 如果输入的数字
我的问题是我不确定如何为此设置变量以及如何准确添加循环以重复该过程 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.parseIntdocs.oracle.com/javase/7/docs/api/java/lang/… -
@PaulNikonowicz 如果用户输入了错误的号码,你想怎么办?
-
为什么要投反对票?我们曾经都是初学者。
-
你并不是真的在问一个问题,而是试图让别人为你写一个程序。对我来说,很明显你不知道如何解决这个问题。您应该阅读/学习更多基本的 Java 教程。