好的,所以這是我第一次發佈。嘗試輸入時沒有此類元素異常
我想創建一個盒子,使用另一個類創建一個矩形。但是當我嘗試運行時,我會輸入高度和寬度的值,但在嘗試輸入深度後,彈出此錯誤。
在此先感謝您的幫助。
控制檯:
Ange rektangelns bredd:
10
Ange rektangelns höjd:
10
En rektangelns med bredden 10 och höjden 10 ger arean 100
Ange rektangelns djup:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at cj222qu.Box.<init>(Box.java:18)
at cj222qu.Steg4_lab02.main(Steg4_lab02.java:7)
Box類:
import java.util.Scanner;
public class Box extends Rectangle {
private int depth;
public Box() {
Scanner hej = new Scanner(System.in);
String dep = null;
boolean go3 = true;
while (go3) {
try {
System.out.println("Ange rektangelns djup: ");
dep = hej.next();
Integer.parseInt(dep);
go3 = false;
} catch (NumberFormatException e) {
System.out.println("Ett fel har inträffat! Ange värdet som ett tal");
go3 = true;
}
}
//new Box(getWidth(), getHeight(), Integer.parseInt(dep));
hej.close();
}
public Box(int width, int height, int depth) {
setDepth(depth);
System.out.println(toString());
}
public String toString() {
StringBuilder result = new StringBuilder();
result.append("En låda med bredden " + getWidth() + ", höjden " + getHeight() + " och djupet " + ".");
result.append("Lådans volym är " + computeVolume() + ".");
result.append("Lådans mantelarea är " + computeArea() + ".");
return result.toString();
}
public int computeVolume() {
int volume = 0;
volume = getWidth() * getHeight() * getDepth();
return volume;
}
public int computeArea() {
int mantelarea = 0;
mantelarea = getDepth() * getWidth() * 2 + getDepth() * getHeight() * 2 + getWidth() * getHeight() * 2;
return mantelarea;
}
public int getDepth()
{
return depth;
}
public void setDepth(int d)
{
depth = d;
}
}
Rectangle類:
import java.util.Scanner;
public class Rectangle {
private int height;
private int width;
public Rectangle(int width, int height)
{
setHeight(height);
setWidth(width);
System.out.println(toString());
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("En rektangelns med bredden " + width + " och höjden " + height + " ger arean " + computeArea());
return result.toString();
}
public Rectangle()
{
Scanner keyboard = new Scanner(System.in);
String w = null;
String h = null;
boolean go1 = true;
boolean go2 = true;
while (go1) {
try {
System.out.println("Ange rektangelns bredd: ");
w = keyboard.next();
Integer.parseInt(w);
go1 = false;
} catch (NumberFormatException e) {
System.out.println("Ett fel har inträffat! Ange värdet som ett tal");
go1 = true;
}
}
while (go2) {
try {
System.out.println("Ange rektangelns höjd: ");
h = keyboard.next();
Integer.parseInt(h);
go2 = false;
} catch (NumberFormatException e) {
System.out.println("Ett fel har inträffat! Ange värdet som ett tal");
go2 = true;
}
}
new Rectangle(Integer.parseInt(w), Integer.parseInt(h));
keyboard.close();
}
public int computeArea()
{
int area = 0;
area = getHeight() * getWidth();
return area;
}
public int getHeight()
{
return height;
}
public int getWidth()
{
return width;
}
public void setHeight(int h)
{
height = h;
}
public void setWidth(int w)
{
width = w;
}
}
主營:
public class Steg4_lab02 {
public static void main(String[] args) {
new Box();
}
}
如果您沒有完成掃描儀掃描,請不要關閉掃描儀。 –
我建議,下次您提問時,您有更多解釋性變量名稱,輸出字符串是英文的。你應該幫助人們幫助你。這通常是更好的編碼方式。如果您希望輸出字符串使用不同的語言,我建議您先從英文開始,然後在第一個公開版本之前更改它們。 –
我認爲收集構造函數中的用戶輸入是一個糟糕的設計。 – Fildor