그래서 나는이 프로젝트를 얼마 동안 작업 해 왔으며 어떤 이유로 든 Java를 잡는 데 많은 어려움을 겪고 있습니다.Java 컴파일러 (접근 자, 돌연변이 유발 자, 생성자)
목표는 각각 3 개의 과일을 포함하는 3 개의 개체를 만들고 각 과일마다 가격/가치가 있습니다.
현재 값을 더하는 데 어려움이 있습니다. 지금까지 자바에 많은 문제가 있다는 말처럼 훨씬 더 잘못되었습니다.
가장 큰 문제는 현재 costofBox()
입니다.
도움이되는 제안 사항에 대해서는 매우 감사 드리며, 일주일 넘게이 작업을 진행하고 있습니다.
public class Project8
{
private String fruit1;
private String fruit2;
private String fruit3;
private String Bundle1;
private String Bundle2;
private String Bundle3;
private int costofBox;
double total;
int broccoli;
int tomato;
int kiwi;
int kale;
int orange;
public String toString()
{
String output = "The box contains: " + Bundle1 + ", " + Bundle2 + ", " + Bundle3 +
"and the cost is $" + costofBox();
return output;
}
public String getBundle1()
{
return Bundle1;
}
public String getBundle2()
{
return Bundle2;
}
public String getBundle3()
{
return Bundle3;
}
public void setBundle1(String Bundle1)
{
Bundle1=fruit1;
}
public void setBundle2(String Bundle2)
{
Bundle2=fruit2;
}
public void setBundle3(String Bundle3)
{
Bundle3=fruit3;
}
public double costofBox()
{
double total=0;
if(Bundle1.equals("broccoli"))
total+=6;
else if(Bundle1.equals("tomato"))
total+=5;
else if(Bundle1.equals("kiwi"))
total+=8;
else if(Bundle1.equals("kale"))
total+=4;
else if(Bundle1.equals("orange"))
total+=7;
if(Bundle2.equals("broccoli"))
total+=6;
else if(Bundle2.equals("tomato"))
total+=5;
else if(Bundle2.equals("kiwi"))
total+=8;
else if(Bundle2.equals("kale"))
total+=4;
else if(Bundle2.equals("orange"))
total+=7;
if(Bundle3.equals("broccoli"))
total+=6;
else if(Bundle3.equals("tomato"))
total+=5;
else if(Bundle3.equals("kiwi"))
total+=8;
else if(Bundle3.equals("kale"))
total+=4;
else if(Bundle3.equals("orange"))
total+=7;
return total;
}
public Project8()
{
fruit1 = "broccoli" + "kale" + "orange";
fruit2 = "kale" + "kiwi" + "orange";
fruit3 = "broccoli" + "tomato" + "kiwi";
}
public Project8(String fruit1, String fruit2, String fruit3)
{
String Bundle1=fruit1;
String Bundle2=fruit2;
String Bundle3=fruit3;
}
public static void main (String [] args)
{
Project8 Bundle1=new Project8 ("broccoli", "kale", "orange");
Project8 Bundle2=new Project8 ("kale", "kiwi", "orange");
Project8 Bundle3=new Project8 ("broccoli", "tomato", "kiwi");
System.out.println("Week 1: " + Bundle1.toString());
System.out.println("Week 2: " + Bundle2.toString());
System.out.println("Week 3: " + Bundle3.toString());
System.out.println("Week4: The box contains:,, and the cost is $0.0");
}
}
가 나를 도울 수있는 당신의 사람들을 위해 시간을 미리 감사 :
여기에 전체 프로그램입니다!
'Map'을 사용하여 각 항목의 가격을 표현하는 것이 더 좋을 것 같습니다. –
'for'loop과'while'loop을보아야합니다 – jhamon
'Bundle1'과 같은 변수는 항상 소문자로 시작해야합니다. 이것을 강제하는 것은 없지만 코드를 더 쉽게 이해할 수 있도록 널리 사용되는 규칙입니다. 마찬가지로 수업은 대문자로 시작해야합니다. – Michael