[英]How can I request the user to select five or less variables to define?
I'm having trouble understanding an assignment problem, specifically the problem asks me to create five integers... but I'd like to implement a way to make it 5 or less, if the user chooses. 我在理解分配问题时遇到问题,特别是该问题要求我创建5个整数...但是我想实现一种方法,以使它小于等于5(如果用户选择)。 How can I do this?
我怎样才能做到这一点? I apologize, I'm new to Java.
抱歉,我是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 ") ;
Thank you for any assistance! 感谢您的协助!
You could put the input in a loop until you meet the requirements you are looking for and then set the size of your array to the input: 您可以将输入置于循环中,直到满足要查找的要求,然后将数组的大小设置为输入:
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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.