![](/img/trans.png)
[英]Unable to find bundled Java version, I get errors in my development so i cant move forward
[英]There are hidden errors that I cant find
我已經完成了我的代碼並且它可以正確打印,但是我有一個測試類,我的類代碼的某些部分沒有打印它應該的內容,我可以請一些幫助。 我嘗試了很多方法來修復它,但它不起作用。
測試類:
public class StoreTester
{
public static void main(String[] args)
{
//1. First Constructor and toString
Store store1 = new Store("grocery");
System.out.println("1. \nOUTPUT: Grocery store is open from 10am to
9pm");
System.out.println("YOUR OUTPUT: "+store1);
System.out.println();
//2. Second constructor and toString
Store store2 = new Store("pet", 8, 18);
System.out.println("2.\nOUTPUT: Pet store is open from 8am to 6pm");
System.out.println("YOUR OUTPUT: "+store2);
System.out.println();
//3. getType
Store store3 = new Store ("grocery", 0, 23);
System.out.println("3.\nOUTPUT: grocery");
System.out.println("YOUR OUTPUT: "+store3.getType());
System.out.println();
//4. getNumHoursOpen
Store store4 = new Store ("pet", 7, 20);
System.out.println("4.\nOUTPUT: 13");
System.out.println("YOUR OUTPUT: "+store4.getNumHoursOpen());
System.out.println();
//5. isOpen (true - store is open)
Store store5 = new Store ("grocery");
System.out.println("5.\nOUTPUT: true");
System.out.println("YOUR OUTPUT: "+store5.isOpen(12));
System.out.println();
//6. isOpen (false - store is closed)
Store store6 = new Store ("pet", 12,20);
System.out.println("6.\nOUTPUT: false");
System.out.println("YOUR OUTPUT: "+store6.isOpen(11));
System.out.println();
//7a. setType - invalid
Store store7 = new Store("pet");
System.out.println("7a.\nOUTPUT: false");
System.out.println("YOUR OUTPUT: "+store7.setType("card"));
System.out.println();
//7b. type not changed
System.out.println("7b.\nOUTPUT: pet");
System.out.println("YOUR OUTPUT: "+store7.getType());
System.out.println();
//8a. setType - valid
Store store8 = new Store("pet");
System.out.println("8a.\nOUTPUT: true");
System.out.println("YOUR OUTPUT: "+store8.setType("grocery"));
System.out.println();
//8b. type changed
System.out.println("8b.\nOUTPUT: grocery");
System.out.println("YOUR OUTPUT: "+store8.getType());
System.out.println();
//9. convertTime - am
System.out.println("9.\nOUTPUT: 11am");
System.out.println("YOUR OUTPUT: "+Store.convertTime(11));
System.out.println();
//10. convertTime - pm
System.out.println("10.\nOUTPUT: 5pm");
System.out.println("YOUR OUTPUT: "+Store.convertTime(17));
System.out.println();
//11a. setHours - valid
Store store11 = new Store("grocery");
System.out.println("11a.\nOUTPUT: true");
System.out.println("YOUR OUTPUT: "+store11.setHours(5,23));
System.out.println();
//11b. Check if new Hours were set
System.out.println("11b.\nOUTPUT: Grocery store is open from 5am to
11pm");
System.out.println("YOUR OUTPUT: " + store11);
System.out.println();
//12a. setHours - invalid
Store store12 = new Store("pet");
System.out.println("12a.\nOUTPUT: false");
System.out.println("YOUR OUTPUT: "+store12.setHours(11,24));
System.out.println();
//12b. Hours should not have changed
System.out.println("12b.\nOUTPUT: Pet store is open from 10am to 9pm");
System.out.println("YOUR OUTPUT: " + store12);
System.out.println();
//13a. setHours - invalid
Store store13 = new Store("pet");
System.out.println("13a.\nOUTPUT: false");
System.out.println("YOUR OUTPUT: "+store13.setHours(12,4));
System.out.println();
//13b. Hours should not have changed because open is after close
System.out.println("13b.\nOUTPUT: Pet store is open from 10am to 9pm");
System.out.println("YOUR OUTPUT: " + store13);
System.out.println();
}
}
這是我的班級有問題。
// type of store (String)
// opening time (int)
// closing time (int)
private String type;
private int opening;
private int closing;
private int hours;
// First constructor: One parameter(store type), opening time set to 10 (10am) and closing time set to 21 (9pm)
public Store(String type)
{
opening = 10;
closing = 21;
}
// Second constructor: 3 parameters: store type, opening time and closing time
public Store(String _type, int _openTime, int _closeTime)
{
type = _type;
opening = _openTime;
closing = _closeTime;
}
// Return the type of store
public String getType()
{
return type;
}
// Return the number of hours open.
// For example, if the store opens at 10 and closes at 18, the number of hours open is 8
public int getNumHoursOpen()
{
hours = closing - opening;
return hours;
}
// If the store is open at currentTime, return true
// If the store is not open at currentTime, return false
public boolean isOpen(int currentTime)
{
if((currentTime >= opening) || (currentTime < closing))
{
return true;
}
else
{
return false;
}
}
// If newType is Grocery or Pet (not case sensitive), set the type and return true;
// Otherwise, the type remains unchanged and false is returned.
public boolean setType(String newType)
{
type = newType;
return true;
}
// if newOpen and newClose are both in the range [0,23] and the opening time is before the closing time,
// set the open and close times to the new times and return true.
// Otherwise, no times are changed and return false
public boolean setHours(int newOpen, int newClose)
{
opening = newOpen;
closing = newClose;
return true;
}
// Converts timeToConvert from 24 hour time to 12 hour time.
// For example, convertTime(5) will return the value 5am
// convertTime(14) will return the value 2pm.
// Note: This method is static. That means that if you are testing this
method from another program, your method
// call will be similar to: Store.convertTime(15);
// If you want to call this method from within the Store class, you can call
it like normal. For example: convertTime(14)
public static String convertTime(int timeToConvert)
{
if (timeToConvert >= 13){
timeToConvert = timeToConvert % 12;
}
return timeToConvert + "";
}
// toString - returns <store type> store is open from <open time> to <close time>
// For example: grocery store is open from 10am to 11pm
// Note: call your convertTime method to convert the time to am and pm
// Since the convertTime method is in the same class, it can be called by just using the name of the method and the time to convert
// For example: convertTime(15) -- the 15 can be replaced by any int
variable. A string will be returned from the convertTime method
public String toString()
{
return type;
}
}
這就是它的打印內容,因為您可以看到有些與他們應該打印的內容不符。
1.
OUTPUT: Grocery store is open from 10am to 9pm
YOUR OUTPUT: null
2.
OUTPUT: Pet store is open from 8am to 6pm
YOUR OUTPUT: pet
3.
OUTPUT: grocery
YOUR OUTPUT: grocery
4.
OUTPUT: 13
YOUR OUTPUT: 13
5.
OUTPUT: true
YOUR OUTPUT: true
6.
OUTPUT: false
YOUR OUTPUT: true
7a.
OUTPUT: false
YOUR OUTPUT: true
7b.
OUTPUT: pet
YOUR OUTPUT: card
8a.
OUTPUT: true
YOUR OUTPUT: true
8b.
OUTPUT: grocery
YOUR OUTPUT: grocery
9.
OUTPUT: 11am
YOUR OUTPUT: 11
10.
OUTPUT: 5pm
YOUR OUTPUT: 5
11a.
OUTPUT: true
YOUR OUTPUT: true
11b.
OUTPUT: Grocery store is open from 5am to 11pm
YOUR OUTPUT: null
12a.
OUTPUT: false
YOUR OUTPUT: true
12b.
OUTPUT: Pet store is open from 10am to 9pm
YOUR OUTPUT: null
13a.
OUTPUT: false
YOUR OUTPUT: true
13b.
OUTPUT: Pet store is open from 10am to 9pm
YOUR OUTPUT: null
isOpen(int time)
的條件錯誤。 您正在使用或||
, 而您應該使用 and &&
,否則如果第一個條件為真,則不會檢查第二個條件,因此所有大於關閉時間的值都將被標記為真。
將其更改為
public boolean isOpen(int currentTime)
{
if((currentTime >= opening) && (currentTime < closing))
{
return true;
}
else
{
return false;
}
}
Case1 寫得不好。 您正在嘗試直接打印對象; 它應該返回與對象實例相關的代碼之類的東西。 你想要的應該是傳遞給 toString 方法的東西。 此外,僅接收 1 個 String 類型參數的構造函數不分配類型,僅打開和關閉。 應該是這樣的
public Store(String type)
{
this.opening = 10;
this.closing = 21;
this.type = type;
}
代碼中有很多小錯誤。 很難分別解釋所有這些。 檢查這些課程並與您的課程進行比較。
public class StoreTester {
public static void main(String[] args) {
//1. First Constructor and toString
Store store1 = new Store("Grocery");
System.out.println("1. \nOUTPUT: Grocery store is open from 10am to 9pm");
System.out.println("YOUR OUTPUT: " + store1);
System.out.println();
//2. Second constructor and toString
Store store2 = new Store("Pet", 8, 18);
System.out.println("2.\nOUTPUT: Pet store is open from 8am to 6pm");
System.out.println("YOUR OUTPUT: " + store2);
System.out.println();
//3. getType
Store store3 = new Store("grocery", 0, 23);
System.out.println("3.\nOUTPUT: grocery");
System.out.println("YOUR OUTPUT: " + store3.getType());
System.out.println();
//4. getNumHoursOpen
Store store4 = new Store("pet", 7, 20);
System.out.println("4.\nOUTPUT: 13");
System.out.println("YOUR OUTPUT: " + store4.getNumHoursOpen());
System.out.println();
//5. isOpen (true - store is open)
Store store5 = new Store("grocery");
System.out.println("5.\nOUTPUT: true");
System.out.println("YOUR OUTPUT: " + store5.isOpen(12));
System.out.println();
//6. isOpen (false - store is closed)
Store store6 = new Store("pet", 12, 20);
System.out.println("6.\nOUTPUT: false");
System.out.println("YOUR OUTPUT: " + store6.isOpen(11));
System.out.println();
//7a. setType - invalid
Store store7 = new Store("pet");
System.out.println("7a.\nOUTPUT: false");
System.out.println("YOUR OUTPUT: " + store7.setType("card"));
System.out.println();
//7b. type not changed
System.out.println("7b.\nOUTPUT: pet");
System.out.println("YOUR OUTPUT: " + store7.getType());
System.out.println();
//8a. setType - valid
Store store8 = new Store("pet");
System.out.println("8a.\nOUTPUT: true");
System.out.println("YOUR OUTPUT: " + store8.setType("grocery"));
System.out.println();
//8b. type changed
System.out.println("8b.\nOUTPUT: grocery");
System.out.println("YOUR OUTPUT: " + store8.getType());
System.out.println();
//9. convertTime - am
System.out.println("9.\nOUTPUT: 11am");
System.out.println("YOUR OUTPUT: " + Store.convertTime(11));
System.out.println();
//10. convertTime - pm
System.out.println("10.\nOUTPUT: 5pm");
System.out.println("YOUR OUTPUT: " + Store.convertTime(17));
System.out.println();
//11a. setHours - valid
Store store11 = new Store("grocery");
System.out.println("11a.\nOUTPUT: true");
System.out.println("YOUR OUTPUT: " + store11.setHours(5, 23));
System.out.println();
//11b. Check if new Hours were set
System.out.println("11b.\nOUTPUT: Grocery store is open from 5am to 11pm");
System.out.println("YOUR OUTPUT: " + store11);
System.out.println();
//12a. setHours - invalid
Store store12 = new Store("pet");
System.out.println("12a.\nOUTPUT: false");
System.out.println("YOUR OUTPUT: " + store12.setHours(11, 24));
System.out.println();
//12b. Hours should not have changed
System.out.println("12b.\nOUTPUT: Pet store is open from 10am to 9pm");
System.out.println("YOUR OUTPUT: " + store12);
System.out.println();
//13a. setHours - invalid
Store store13 = new Store("pet");
System.out.println("13a.\nOUTPUT: false");
System.out.println("YOUR OUTPUT: " + store13.setHours(12, 4));
System.out.println();
//13b. Hours should not have changed because open is after close
System.out.println("13b.\nOUTPUT: Pet store is open from 10am to 9pm");
System.out.println("YOUR OUTPUT: " + store13);
System.out.println();
}
}
和
public class Store {
// type of store (String)
// opening time (int)
// closing time (int)
private String type;
private int opening;
private int closing;
private int hours;
// First constructor: One parameter(store type), opening time set to 10 (10am) and closing time set to 21 (9pm)
public Store(String type) {
this.type = type;
opening = 10;
closing = 21;
}
// Second constructor: 3 parameters: store type, opening time and closing time
public Store(String _type, int _openTime, int _closeTime) {
type = _type;
opening = _openTime;
closing = _closeTime;
}
// Return the type of store
public String getType() {
return type;
}
// Return the number of hours open.
// For example, if the store opens at 10 and closes at 18, the number of hours open is 8
public int getNumHoursOpen() {
hours = closing - opening;
return hours;
}
// If the store is open at currentTime, return true
// If the store is not open at currentTime, return false
public boolean isOpen(int currentTime) {
if ((currentTime >= opening) && (currentTime < closing)) {
return true;
} else {
return false;
}
}
// If newType is Grocery or Pet (not case sensitive), set the type and return true;
// Otherwise, the type remains unchanged and false is returned.
public boolean setType(String newType) {
if("grocery".equalsIgnoreCase(newType) || "pet".equalsIgnoreCase(newType)) {
type = newType;
return true;
}
return false;
}
// if newOpen and newClose are both in the range [0,23] and the opening time is before the closing time,
// set the open and close times to the new times and return true.
// Otherwise, no times are changed and return false
public boolean setHours(int newOpen, int newClose) {
if(newOpen < newClose && newClose < 24) {
opening = newOpen;
closing = newClose;
return true;
}
return false;
}
// Converts timeToConvert from 24 hour time to 12 hour time.
// For example, convertTime(5) will return the value 5am
// convertTime(14) will return the value 2pm.
// Note: This method is static. That means that if you are testing this
// method from another program, your method
// call will be similar to: Store.convertTime(15);
// If you want to call this method from within the Store class, you can call
// it like normal. For example: convertTime(14)
public static String convertTime(int timeToConvert) {
if (timeToConvert > 12) {
timeToConvert = timeToConvert - 12;
return timeToConvert + "pm";
}
return timeToConvert + "am";
}
// toString - returns <store type> store is open from <open time> to <close time>
// For example: grocery store is open from 10am to 11pm
// Note: call your convertTime method to convert the time to am and pm
// Since the convertTime method is in the same class, it can be called by just using the name of the method and the time to convert
// For example: convertTime(15) -- the 15 can be replaced by any int
// variable. A string will be returned from the convertTime method
@Override
public String toString() {
String time = convertTime(closing);
return type + " store is open from " + opening + "am to " + time;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.