我无法理解分配问题,特别是问题要求我创建五个整数...但是如果用户选择的话,我想实现一种方法使其达到5或更少。我怎样才能做到这一点?我道歉,我是Java的新手。
package realestatepropvalue;
import java.util.Scanner;
public class RealEstatePropertyValue
{
public static void main(String[] args)
{
Scanner sc = new Scanner ( System.in );
System.out.println ( "Hello friend, you will enter a series "
+ "of fields that'll calculate your Real Estate Property Value.");
System.out.print ("Please enter a Street Number ");
String streetnum = sc.next();
sc.nextLine();
System.out.println ("You entered the Street Number, " +streetnum );
System.out.print ( "Please enter a Street Name ");
String streetnam = sc.nextLine();
System.out.println ("You entered the Street Name, " + streetnam + " " );
System.out.print ("Please enter the number of rooms! (Up to 5!) ");
int roomcount = sc.nextInt();
String[] places = new String[5];
for(int i = 0; i < places.length; i++) {
places[i] = "Place Number: " + i;
}
sc.nextLine();
System.out.println("You said that there were " + roomcount + " rooms!");
System.out.print ("Please enter the types of rooms (up to " +roomcount+ ") that fill up the " + roomcount + " rooms!\n"
+ "(Rooms like Living, Dining, Bedroom1-2, Kitchen, Bathroom, etc!) \n ") ;
感谢您的帮助!
答案 0 :(得分:0)
您可以将输入放在一个循环中,直到满足您要求的要求,然后将数组的大小设置为输入:
System.out.print ("Please enter the number of rooms! (Up to 5!) ");
while(true){
roomCount = sc.nextInt();
if(roomCount <= 5 && roomCount > 0){
break;
}else{
System.out.println("Invalid amount of rooms");
}
}
String[] places = new String[roomCount];