gpt4 book ai didi

Java编程-帮助初始化(地毯)

转载 作者:行者123 更新时间:2023-12-02 00:13:34 26 4
gpt4 key购买 nike

我已经开始学习Java编程,但在做作业时遇到了问题:ps这是一个c编程作业,用它来做一个java编程:

Consider a (potentially large commercial) room that has floor dimensions length len and width wid. The area of the floor is given by multiplying len by wid.

Carpet tiles are provided as individual squares (2 feet by 2 feet). Write, compile and run a C program, carpet.c, that

-calculates the minimum number of carpet tiles that are needed to cover a room whose dimensions (len and wid) are input from the terminal (measurements are in inches). NB: floor tiles can be cut to fit at the edges of the room - try to minimise waste.

-calculates how many carpet-tile packs are needed. The supplier only provides carpet tiles in packs of ten (i.e. individual tiles cannot be purchased separately).

-calculates how many spare tiles are left over once the minimum number of packs have been purchased.

-prints out all these results in a neat table along with the total cost of the order. (A pack of tiles costs $50 for each of the first four packs, and the price reduces to $45 for each subsequent pack.)

Think carefully about how are you going to test your program? The calculations are non-trivial and easy to get wrong. If your program doesn't work then you will waste the company a lot of money and you will probably lose the contract to supply the software.

You should write out a number of test cases (by hand) that cover all the different possibilities that could happen. Don't forget to consider various boundary cases too - these are often where errors are detected.

到目前为止我已经做了:

import java.util.Scanner;

public class carpet {
public static void main (String args[]){
Scanner scanf = new Scanner (System.in);

float len, wid;
float area;

int roundTiles;
int roundPacks;

float tarea;
float tpack;
float NoOfTiles;
float NoOfPacks;
float tspares1;
float tspares2;
int packCost;
int cost;

tarea= 12* 12;
tpack= 10;

System.out.format("Enter the length of the room, Inches: ");
len = scanf.nextFloat();

System.out.format("Enter the width of the room, Inches: ");
wid = scanf.nextFloat();

area = len * wid;

NoOfTiles = area/ tarea;
NoOfPacks = NoOfTiles/tpack;


roundTiles = (int) Math.ceil(NoOfTiles);
roundPacks = (int) Math.ceil(NoOfPacks);



tspares1 = roundPacks * 10;
tspares2 = tspares1 - roundTiles;

if (roundPacks <= 4)
packCost =50;
else if(roundPacks > 4)
{
packCost = 45;
packCost = packCost + 20; *<<-----******LINE 50-----*********

}


cost =roundPacks * packCost; *<<*******---ERROR-------------*********

System.out.println(cost);

}

}

错误提示: “局部变量 packCost 可能尚未初始化”

编译器说: “线程“main”java.lang.Error中出现异常: Unresolved 编译问题: 局部变量 packCost 可能尚未初始化 在地毯.main(地毯.java:50)“

最佳答案

您需要通过替换当前行来初始化变量packCost:

int packCost;

int packCost=0;

您正在条件语句中初始化 packCost 变量,而编译器不够智能,无法检测到它。

关于Java编程-帮助初始化(地毯),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12306255/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com