程序从用户处获取一个整数,并从用户处获取一行,并检查它们是否为整数,并保留它们是否为整数。
我想在循环中从用户那里得到一行,检查每个值是否为整数,然后将其放入字符串中。(稍后我将使用这些值作为整数)。我尝试从第一个整数中获取值,但仍然不起作用(?)(我将其打印出来并得到0)这是我的整体工作:
package spaceship;
import java.util.Scanner;
public class SpaceShip {
static int meteorNumber; //For taking the numbers of meteors in the beginning of the game.
static String firstLine; //For taking the values of the first line.
static String secondLine; //For taking the values of the second line.
static int i; //Used for the loops.
public static void gamePlace() {
Scanner get = new Scanner(System.in); //For taking the values from user.
System.out.println("Please enter your desired number of meteors and the first line:");
meteorNumber = get.nextInt();
for(i=0;i<7*meteorNumber;i++) {
if(get.hasNextInt() || get.hasNextDouble())
firstLine += String.valueOf(i);
}
System.out.println(firstLine);
}
public static void main(String[] args) {
gamePlace();
}
}
您的代码有很多问题首先用扫描仪输入。请参见here。第二个是static String firstLine;
,您必须像static String firstLine = ""
一样先声明。
祝你好运@@