如何从每个循环中保存项目?

时间:2017-10-27 23:37:58

标签: java logic

我为我的问题不太清楚而道歉,我将尝试通过我的代码解释它。

整个代码:

package realestatepropvalue;

import java.util.Scanner;

public class RealEstatePropValueA3 {

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[roomcount];
    for (int i = 0; i < places.length; i++) {
        places[i] = "Place Number: " + (i + 1);
    }

    sc.nextLine();

    System.out.println("You said that there were " + roomcount + " rooms!");

    System.out.println("What is the price per sq. feet?");
    int pricePer = sc.nextInt();
    sc.nextLine(); //This will consume the end of the line.


    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 ");
    int squareCounter = 0;
    String[] types = new String[roomcount];

    for (int i = 0; i < types.length; i++) {
        System.out.print("Please enter the types of room " + places[i] + ": ");
        String roomName = sc.nextLine();
        types[i] = roomName;

        System.out.print("Please enter the square feet of " + roomName + ": ");
        int squareFeet = sc.nextInt();
        sc.nextLine(); //This will consume empty line.

        squareCounter = squareFeet + squareCounter;

        System.out.println("Room name: " + roomName + " is: " + squareFeet + "square feet.");


        int finalCalc = pricePer * squareCounter;




        System.out.println("\n"); // make space for a cleaner format
        System.out.println("1.                   \t      Street: " + streetnam + " #" + streetnum);
        System.out.println("2.                   \t Total Rooms: " + roomcount);
        System.out.println("3.                   \t  Total Area: " + squareCounter + "sq. ft.");
        System.out.println("4.           \t    Price per sq. ft: " + pricePer);
        System.out.println("5.   \t    Estimated property value: " + finalCalc + "\n");
    }



    // TODO code application logic here
}//main

}//class RealEstatePropValueA3

输出:

Hello friend, you will enter a series of fields that'll calculate your Real Estate Property Value.
Please enter a Street Number 7 **<-- user input**
You entered the Street Number, 7
Please enter a Street Name Washington Street **<-- user input**
You entered the Street Name, Washington Street 
Please enter the number of rooms! (Up to 5!) 2 **<-- user input**
You said that there were 2 rooms!
What is the price per sq. feet?
150 **<-- user input**
Please enter the types of rooms (up to 2) that fill up the 2 rooms!
(Rooms like Living, Dining, Bedroom1-2, Kitchen, Bathroom, etc!) 
 Please enter the types of room Place Number: 1: Kitchen **<-- user input**
Please enter the square feet of Kitchen: 15 **<-- user input**
Room name: Kitchen is: 15square feet.


1.                            Street: Washington Street #7
2.                       Total Rooms: 2
3.                        Total Area: 15sq. ft.
4.                  Price per sq. ft: 150
5.          Estimated property value: 2250

Please enter the types of room Place Number: 2: enter code here **this is where I'd continue**

所需的输出:

1.                            Street: Washington Street #7
2.                       Total Rooms: 2 (All entered rooms would output here)
3.                        Total Area: 15sq. ft.
4.                  Price per sq. ft: 150
5.          Estimated property value: 2250

我只是需要帮助以某种方式收集所有用户输入的房间,并将它们输出到2号(在括号中)。

此外,作业要求&#34;您的程序应将行数保存在单独的变量中,并使用该变量在报告中打印行号,如下所示:&#34;虽然我手动做了...我知道这是错的。

我怎么能实现这个目标?我不明白如何将其转换为java代码。我对此非常陌生,谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

首先,您应该使用float或double数据类型声明保存货币值的变量。处理平方英尺的变量也应该声明为float或double。这些变量将是 pricePer finalCalc squareFeet squarecCounter 。在从用户收集信息以填充任何这些变量时,您还应该使用 Scanner.nextDouble()方法。您将获得更准确的整体计算。

最大房间数 5 是不现实的。房间应该没有限制。允许用户确定此最大值。您只需确保通过 for 循环处理每个房间。

显示整体结果的最终控制台显示应 循环来处理房间。

完成后,您的代码可能看起来像这样:

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 address Home Number (ex. 12345): ");
String streetnum = sc.nextLine();
System.out.println("You entered the Home 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: ");
int roomcount = sc.nextInt();
String[] rooms = new String[roomcount];
for (int i = 0; i < rooms.length; i++) {
    rooms[i] = "Room Number: " + (i + 1);
}
sc.nextLine();

System.out.println("You said that there were " + roomcount + " rooms!");

System.out.println("What is the price per sq. feet?");
double pricePer = sc.nextDouble();
sc.nextLine(); //This will consume the end of the line.

System.out.println("Please enter the room names (up to " + roomcount + ") that make up the home!\n"
        + "(Rooms like Living, Dining, Bedroom1-2, Kitchen, Bathroom, etc!)\n");
double squareCounter = 0.0;
double finalCalc = 0.0;
String[] roomNames = new String[roomcount];

for (int i = 0; i < roomNames.length; i++) {
    System.out.print("Please enter the name of room " + rooms[i] + ": ");
    String roomName = sc.nextLine();
    roomNames[i] = roomName;
    System.out.print("Please enter the square feet of " + roomName + ": ");
    double squareFeet = sc.nextDouble();
    sc.nextLine(); //This will consume empty line.
    squareCounter = squareFeet + squareCounter;
    System.out.println("Room name: " + roomName + " is: " + squareFeet + " square feet.\n");
}

finalCalc = pricePer * squareCounter;
System.out.println("\n"); // make space for a cleaner format
System.out.println("1.                   \t      Street: " + streetnam + " #" + streetnum);
System.out.println("2.                   \t Total Rooms: " + roomcount);
System.out.println("3.                   \t  Total Area: " + squareCounter + " sq. ft.");
System.out.println("4.           \t    Price per sq. ft: " + 
        NumberFormat.getCurrencyInstance(new Locale("en", "US")).format(pricePer));
System.out.println("5.   \t    Estimated property value: " +
        NumberFormat.getCurrencyInstance(new Locale("en", "US")).format(finalCalc) + "\n");