简体   繁体   English

如何请求用户选择五个或更少的变量进行定义?

[英]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.

相关问题 如何使用条件运算符获取五个变量中的最高值和最低值? - How can I use conditional operators to get the highest and lowest value among the five variables? 如何在NetBeans中为Java项目定义环境变量? - How can I define environment variables for a Java project in NetBeans? 我怎么可能使用较少的变量? - How could I possibly use less variables? 如何避免每五秒钟进行一次新连接? - How can I avoid making a new conection every five seconds? 如何从用户请求中获取用户代理? - How can I get a User-agent from user request? 如果节点创建的时间戳少于或超过五分钟,我应该如何使服务器检查? - How should I make the server check, if the time stamp made by the node is less than or more than five minutes old? 如何在Java中的Struts应用程序中将自定义变量保存到请求中? - How can I save custom variables into the request in a Struts application in Java? 如果传入请求的字段少于或多于 POJO,我如何告诉 Hibernate 验证器抛出异常 - How can I tell Hibernate Validator to throw exception if the incoming request has less or extra fields than the POJO 我该如何定义这样的数组? - How can I define such an array? 如何仅对用户自己的端点启用请求 - How can I enable request only for user's own endpoint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM