看来java正在取代我的对象而不是另一个,我该如何补救
该项目将创建一个应用程序,模拟一家冰淇淋店,跟踪打开的纸箱数量,并允许使用两种不同大小的勺子,教授希望所有这些都像我所做的那样分成几类 这是我的主要方法,它创建并调用我的scoop类看来java正在取代我的对象而不是另一个,我该如何补救,java,object,Java,Object,该项目将创建一个应用程序,模拟一家冰淇淋店,跟踪打开的纸箱数量,并允许使用两种不同大小的勺子,教授希望所有这些都像我所做的那样分成几类 这是我的主要方法,它创建并调用我的scoop类 import java.util.Scanner; public class Application { public static void main(String[] args) { //Creating Objects Scanner input = new Scanner(System.
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
//Creating Objects
Scanner input = new Scanner(System.in);
Scoop scoop = new Scoop();
IceCreamShoppe icecreamshoppe = new IceCreamShoppe();
//Prompting user to define sizes of scoops and carton
System.out.println("How big is the first ice cream scoop?(radius in cm's)");
scoop.setScoop1(input.nextDouble());
System.out.println("How big is the second ice cream scoop?(radius in cm's)");
scoop.setScoop2(input.nextDouble());
System.out.println("How big is the ice cream carton?(radius in cm's)");
icecreamshoppe.setCarton_radius(input.nextDouble());
System.out.println("How big is the ice cream carton?(height in cm's)");
icecreamshoppe.setCarton_height(input.nextDouble());
//Returns to user their set values and sets size of carton
System.out.printf("Scoop 1: %.2f, Scoop 2: %.2f, Carton Radius: %.2f, Carton Height %.2f%n", scoop.getScoop1(), scoop.getScoop2(), icecreamshoppe.getCarton_radius(), icecreamshoppe.getCarton_height());
//While statement that continuously prompts user which scoop they want to use, everything else is done in the IceCreamShoppe Class
while(true) {
System.out.println("Which scoop would you like to use? (1 or 2) (0 = Exit)");
int scoopNumber = input.nextInt();
System.out.println("How many scoops would you like?");
int scoopCount = input.nextInt();
icecreamshoppe.serve(scoopNumber, scoopCount);
}//end while
}//end main
}//class
public class Scoop {
//Instance Variables
private double volume;
private double scoop1;
private double scoop2;
//Constructors
public double doScoop(int r) {
System.out.printf("You want this%f", this.getScoop1());
if(r == 1) {
volume = (Math.PI)*Math.pow(this.getScoop1(), 3)*(4.0/3.0);
this.setVolume(volume);
System.out.printf("%f", volume);
return volume;
}
else if(r == 2) {
volume = (Math.PI)*Math.pow(this.getScoop2(), 3)*(4.0/3.0);
this.setVolume(volume);
System.out.printf("%f", volume);
return volume;
}
else {
System.out.println("Invalid Scoop Number!");
return 0;
}
}
/**
* @return the volume
*/
public double getVolume() {
return volume;
}
/**
* @param volume the volume to set
*/
public void setVolume(double volume) {
this.volume = volume;
}
/**
* @return the scoop1
*/
public double getScoop1() {
return scoop1;
}
/**
* @param scoop1 the scoop1 to set
*/
public void setScoop1(double scoop1) {
this.scoop1 = scoop1;
}
/**
* @return the scoop2
*/
public double getScoop2() {
return scoop2;
}
/**
* @param scoop2 the scoop2 to set
*/
public void setScoop2(double scoop2) {
this.scoop2 = scoop2;
}
//
}//end class
这是我的scoop方法,它自己调用,我认为这是问题的根源。问题是,每当我调用scoop类时,我的勺子大小总是被重置
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
//Creating Objects
Scanner input = new Scanner(System.in);
Scoop scoop = new Scoop();
IceCreamShoppe icecreamshoppe = new IceCreamShoppe();
//Prompting user to define sizes of scoops and carton
System.out.println("How big is the first ice cream scoop?(radius in cm's)");
scoop.setScoop1(input.nextDouble());
System.out.println("How big is the second ice cream scoop?(radius in cm's)");
scoop.setScoop2(input.nextDouble());
System.out.println("How big is the ice cream carton?(radius in cm's)");
icecreamshoppe.setCarton_radius(input.nextDouble());
System.out.println("How big is the ice cream carton?(height in cm's)");
icecreamshoppe.setCarton_height(input.nextDouble());
//Returns to user their set values and sets size of carton
System.out.printf("Scoop 1: %.2f, Scoop 2: %.2f, Carton Radius: %.2f, Carton Height %.2f%n", scoop.getScoop1(), scoop.getScoop2(), icecreamshoppe.getCarton_radius(), icecreamshoppe.getCarton_height());
//While statement that continuously prompts user which scoop they want to use, everything else is done in the IceCreamShoppe Class
while(true) {
System.out.println("Which scoop would you like to use? (1 or 2) (0 = Exit)");
int scoopNumber = input.nextInt();
System.out.println("How many scoops would you like?");
int scoopCount = input.nextInt();
icecreamshoppe.serve(scoopNumber, scoopCount);
}//end while
}//end main
}//class
public class Scoop {
//Instance Variables
private double volume;
private double scoop1;
private double scoop2;
//Constructors
public double doScoop(int r) {
System.out.printf("You want this%f", this.getScoop1());
if(r == 1) {
volume = (Math.PI)*Math.pow(this.getScoop1(), 3)*(4.0/3.0);
this.setVolume(volume);
System.out.printf("%f", volume);
return volume;
}
else if(r == 2) {
volume = (Math.PI)*Math.pow(this.getScoop2(), 3)*(4.0/3.0);
this.setVolume(volume);
System.out.printf("%f", volume);
return volume;
}
else {
System.out.println("Invalid Scoop Number!");
return 0;
}
}
/**
* @return the volume
*/
public double getVolume() {
return volume;
}
/**
* @param volume the volume to set
*/
public void setVolume(double volume) {
this.volume = volume;
}
/**
* @return the scoop1
*/
public double getScoop1() {
return scoop1;
}
/**
* @param scoop1 the scoop1 to set
*/
public void setScoop1(double scoop1) {
this.scoop1 = scoop1;
}
/**
* @return the scoop2
*/
public double getScoop2() {
return scoop2;
}
/**
* @param scoop2 the scoop2 to set
*/
public void setScoop2(double scoop2) {
this.scoop2 = scoop2;
}
//
}//end class
这是另外两门课,我不认为它们是问题所在,但话说回来,我知道什么
import java.util.Scanner;
public class IceCreamShoppe {
//Instance Variables
private double carton_radius;
private double carton_height;
private int cartons_used = 0;
//Constructors
public IceCreamShoppe() {
}
public IceCreamShoppe(double r, double h) {
this.setCarton_radius(r);
this.setCarton_height(h);
}
public int carton_used() {
this.setCartons_used(this.getCartons_used() + 1);
return this.getCartons_used();
}
/*
* Object to serve the ice cream, the only input is which scoop is supposed to be used.
* @param s Sets the
*/
public boolean serve(int s, int n) {
Scanner input = new Scanner(System.in);
Scoop scoop = new Scoop();
Carton carton = new Carton();
int i = 0;
//Checks which scoop the is being used
if(s != 0) {
double scoopVolume = scoop.doScoop(1);
while(i <= n) {
if((boolean) carton.hasEnough(scoopVolume)) {
carton.remove(scoopVolume);
System.out.println(carton.getContains());
i++;
}
else {
this.carton_used();
carton.carton(this.getCarton_radius(), this.getCarton_height());
carton.remove(scoopVolume);
System.out.printf("Carton Empty, Total Cartons Used:%d%n", this.getCartons_used());
System.out.printf("New carton contains: %.2f%n", carton.getContains());
}
}
}
else {
System.out.printf("You have used %d cartons of ice cream, there is %.2f ice cream remaining in the current carton", this.getCartons_used(), carton.getContains());
return false;
}
return true;
}
//Getters and Setters
/**
* @return the carton_radius
*/
public double getCarton_radius() {
return carton_radius;
}
/**
* @param carton_radius the carton_radius to set
*/
public void setCarton_radius(double carton_radius) {
this.carton_radius = carton_radius;
}
/**
* @return the carton_height
*/
public double getCarton_height() {
return carton_height;
}
/**
* @param carton_height the carton_height to set
*/
public void setCarton_height(double carton_height) {
this.carton_height = carton_height;
}
/**
* @return the cartons_used
*/
public int getCartons_used() {
return cartons_used;
}
/**
* @param cartons_used the cartons_used to set
*/
public void setCartons_used(int cartons_used) {
this.cartons_used = cartons_used;
}
}//End Class
public class Carton {
//Instance Variables
private double contains;
//Objects
//Checks if the carton has enough remaining, if not
public boolean hasEnough(double v) {
if ((this.getContains() - v) >= 0) {
return true;
}
else {
return false;
} }
//Sets the carton size
public void carton(double r, double h) {
this.setContains(Math.PI*Math.pow(r, 2)*h);
}
//After checking if there is enough ice cream, this will run, there is
no check against going negative
public void remove(double v) {
this.setContains(this.getContains() - v);
}
//Getters and Setters
/**
* @return the contains
*/
public double getContains() {
return contains;
}
/**
* @param contains the contains to set
*/
public void setContains(double contains) {
this.contains = contains;
}
}
import java.util.Scanner;
公共级冰淇淋店{
//实例变量
私人双纸箱半径;
私家双纸箱箱高;
使用的专用整箱纸箱=0;
//建设者
公共冰淇淋店(){
}
公共冰淇淋店(双r,双h){
这是一个半径(r);
这是设置纸箱高度(h);
}
公共int carton_used(){
使用此.setCartons_(使用此.getCartons_()+1);
退回这个。getCartons_used();
}
/*
*要提供冰淇淋,唯一的输入是应该使用哪个勺子。
*@param s设置
*/
公共布尔服务(int s,int n){
扫描仪输入=新扫描仪(System.in);
勺=新勺();
纸箱=新纸箱();
int i=0;
//检查正在使用的勺子
如果(s!=0){
双勺体积=勺。doScoop(1);
while(i=0){
返回true;
}
否则{
返回false;
} }
//设置纸盒大小
公共空纸箱(双r,双h){
这个.setContains(Math.PI*Math.pow(r,2)*h);
}
//在检查是否有足够的冰淇淋后,这将运行,有
不检查是否呈阴性
公共空间移除(双v){
this.setContains(this.getContains()-v);
}
//接球手和接球手
/**
*@returnthecontains
*/
公共双getContains(){
返回包含;
}
/**
*@param包含要设置的内容
*/
公共void setContains(双包含){
this.contains=包含;
}
}
您没有将scoop对象传递到IcecreamShope.serve方法中,然后您正在serve方法中创建一个新的scoop对象
你应该把它交上来:
icecreamshoppe.serve(scoopNumber, scoopCount, scoop);
并更改发球方法如下:
public boolean serve(int s, int n, Scoop scoop) {
Scanner input = new Scanner(System.in);
Carton carton = new Carton();
int i = 0;
//Checks which scoop the is being used
if(s != 0) {
double scoopVolume = scoop.doScoop(1)
“我认为这是问题的根源”-问题是什么?欢迎来到Stackoverflow,请阅读。请特别注意。你在发布一个好问题上投入的精力越大:一个易于阅读、理解的问题,而且它吸引相关人员的几率越高,你获得帮助的速度也就越快。祝你好运!运行
doScoop()
在一个未初始化的独家新闻上
没什么用……你能更具体地说明这个问题吗?什么是“很多”根据你的说法?谢谢,我真的很感激,像这样的事情可能看起来很明显,但对于一个刚刚学习的人来说,这花了我一辈子的时间。知道comp-sci领域有人愿意帮忙真是太棒了。:@ThomasWalker欢迎你!