其實之前就有遇到過Scanner實體化的物件呼叫close出現一些問題,現在可能暴露了出來,
為什么是說“可能”因為我這篇博客也沒給出完整的錯誤分析,就當挖個坑吧…
文章目錄
- 題目
- 過關代碼
- 好了,只想要過關的童鞋可以省略以下內容了,
- 錯誤筆記
- 有問題的代碼(有錯誤)
- 錯誤簡短描述
- 分析程序
- 錯誤簡單分析
- 新的博客已寫完,包含對以上問題的完全剖析
題目
8 實驗8 計算機類 (100 分)
構造計算機類,其中包含其配置資訊:處理器、主板、記憶體、顯示幕、硬碟等設備,各個設備均有型號(字串),特別的,處理器有主頻(小數)和內核數(整數)、顯示幕有尺寸(整型)、記憶體和硬碟有容量資料(GB為單位),請你嘗試構造合適的類和類的關系來表示計算機,并為該計算機類添加計算價格(各設備價格之和)、列印配置資訊等方法,重寫相關類的equals方法,使得兩個配置完全相同的計算機為相同的計算機,重寫相關類的toString函式,列印計算機的配置資訊, 在main函式中
提示: 為計算機類和各個組成配件都構造類,分別重寫toString和equals方法 在計算機類中呼叫各個配件的equals和toString方法 回車用\n 小數用String.format("%.1f",1.234)
輸入格式:
兩個計算機物件,包含 CPU:型號、主頻、內核 主板:型號 記憶體:容量 顯示幕:尺寸 硬碟:容量
輸出格式:
兩個物件是否相等 兩個物件的配置資訊
輸入樣例:
在這里給出一組輸入,例如:
Corei7 2.8 4
GIGABYTE-B250M-D3H
xiede-DDR3 8
SamsungC27F39 27
SEAGATE-ST1000DM010 2048
Corei7 2.8 4
GIGABYTE-B250M-D3H
xiede-DDR3 8
SamsungC27F39 27
SEAGATE-ST1000DM010 2048
輸出樣例:
在這里給出相應的輸出,例如:
true
Computer1:
CPU:
Model: Corei7
Frequency: 2.8
Number of Cores: 4
Mainboard:
Model: GIGABYTE-B250M-D3H
Memory:
Model: xiede-DDR3
Size: 8
Screen:
Model: SamsungC27F39
Size: 27
Harddisk:
Model: SEAGATE-ST1000DM010
Size: 2048
Computer2:
CPU:
Model: Corei7
Frequency: 2.8
Number of Cores: 4
Mainboard:
Model: GIGABYTE-B250M-D3H
Memory:
Model: xiede-DDR3
Size: 8
Screen:
Model: SamsungC27F39
Size: 27
Harddisk:
Model: SEAGATE-ST1000DM010
Size: 2048
過關代碼
import java.util.Scanner;
class Disk{
String model ;
int size;
public Disk(){
this.init();
}
public Disk(String model , int size ){
this.model = model;
this.size = size;
}
private void init(){
this.model = "";
this.size = 0;
}
public String printInfo(){
return "Harddisk:\nModel: " + this.model +
"\nSize: " + this.size + "\n" ;
}
}
class Display{
String model ;
int size;
public Display(){
this.init();
}
public Display(String model , int size){
this.model = model;
this.size = size;
}
private void init(){
this.model = "";
this.size = 0;
}
public String printInfo(){
return "Screen:\nModel: " + this.model +
"\nSize: " + this.size + "\n" ;
}
}
class RAM{
String model ;
int size;
public RAM(){
this.init();
}
public RAM(String model,int size ){
this.model = model ;
this.size = size;
}
private void init(){
this.model = "";
this.size = 0;
}
public String printInfo(){
return "Memory:\nModel: " + this.model +
"\nSize: " + this.size + "\n" ;
}
}
class MotherBoard{
String model ;
public MotherBoard(){
this.init();
}
public MotherBoard(String model){
this.model = model;
}
private void init(){
this.model = "";
}
public String printInfo(){
return "Mainboard:\nModel: " + this.model + "\n" ;
}
}
class CPU{
String model ;
double clockSpeed;
int coreCount;
public CPU(){
this.init();
}
public CPU(String model, double cS,int cC){
this.model = model;
this.clockSpeed = cS;
this.coreCount = cC;
}
private void init(){
this.model = "";
this.clockSpeed = 0.0;
this.coreCount = 0;
}
public String printInfo(){
return "CPU:\nModel: " + this.model +
"\nFrequency: " + this.clockSpeed +
"\nNumber of Cores: " + this.coreCount + "\n" ;
}
}
class Computer{
String info ;
CPU cpu;
MotherBoard motherboard;
RAM ram;
Display display;
Disk disk;
public Computer(Scanner in){
this.info = "";
this.cpu = new CPU(in.next(), in.nextDouble(), in.nextInt());
this.motherboard = new MotherBoard(in.next() );
this.ram = new RAM(in.next(), in.nextInt());
this.display = new Display(in.next(), in.nextInt() );
this.disk = new Disk(in.next(), in.nextInt() );
}
@Override
public String toString(){
return cpu.printInfo() +
motherboard.printInfo() +
ram.printInfo() +
display.printInfo() +
disk.printInfo();
}
@Override
public boolean equals(Object ob){
if( (ob == null) || !(ob instanceof Computer) ){
return false;
}
return ( this.toString().equals(ob.toString()) );
}
}
public class Main{
public static void main(String []arg){
Scanner in = new Scanner(System.in);
Computer c1 = new Computer(in),
c2 = new Computer(in);
System.out.println( c1.equals(c2) );
System.out.println("Computer1:");
System.out.print( c1.toString());
System.out.println("Computer2:");
System.out.print( c2.toString() );
}
}
好了,只想要過關的童鞋可以省略以下內容了,
E.N.D
錯誤筆記
有問題的代碼(有錯誤)
import java.util.Scanner;
class Disk{
String model ;
int size;
public Disk(){
this.init();
}
public Disk(String model , int size ){
this.model = model;
this.size = size;
}
private void init(){
this.model = "";
this.size = 0;
}
public String printInfo(){
return "Harddisk:\nModel: " + this.model +
"\nSize: " + this.size + "\n" ;
}
}
class Display{
String model ;
int size;
public Display(){
this.init();
}
public Display(String model , int size){
this.model = model;
this.size = size;
}
private void init(){
this.model = "";
this.size = 0;
}
public String printInfo(){
return "Screen:\nModel: " + this.model +
"\nSize: " + this.size + "\n" ;
}
}
class RAM{
String model ;
int size;
public RAM(){
this.init();
}
public RAM(String model,int size ){
this.model = model ;
this.size = size;
}
private void init(){
this.model = "";
this.size = 0;
}
public String printInfo(){
return "Memory:\nModel: " + this.model +
"\nSize: " + this.size + "\n" ;
}
}
class MotherBoard{
String model ;
public MotherBoard(){
this.init();
}
public MotherBoard(String model){
this.model = model;
}
private void init(){
this.model = "";
}
public String printInfo(){
return "Mainboard:\nModel: " + this.model + "\n" ;
}
}
class CPU{
String model ;
double clockSpeed;
int coreCount;
public CPU(){
this.init();
}
public CPU(String model, double cS,int cC){
this.model = model;
this.clockSpeed = cS;
this.coreCount = cC;
}
private void init(){
this.model = "";
this.clockSpeed = 0.0;
this.coreCount = 0;
}
public String printInfo(){
return "CPU:\nModel: " + this.model +
"\nFrequency: " + this.clockSpeed +
"\nNumber of Cores: " + this.coreCount + "\n" ;
}
}
class Computer{
String info ;
CPU cpu;
MotherBoard motherboard;
RAM ram;
Display display;
Disk disk;
private void init(){
Scanner in = new Scanner(System.in);
this.info = "";
this.cpu = new CPU(in.next(), in.nextDouble(), in.nextInt());
this.motherboard = new MotherBoard(in.next() );
this.ram = new RAM(in.next(), in.nextInt());
this.display = new Display(in.next(), in.nextInt() );
this.disk = new Disk(in.next(), in.nextInt() );
}
public Computer(){
this.init();
}
@Override
public String toString(){
return cpu.printInfo() +
motherboard.printInfo() +
ram.printInfo() +
display.printInfo() +
disk.printInfo();
}
@Override
public boolean equals(Object ob){
if( (ob == null) || !(ob instanceof Computer) ){
return false;
}
return ( this.toString().equals(ob.toString()) );
}
public String printPrice(){
return "free!";
}
}
public class Main{
public static void main(String []arg){
Computer c1 = new Computer(),
c2 = new Computer();
System.out.println( c1.equals(c2) );
System.out.println("Computer1:");
System.out.print(c1.toString());
System.out.println("Computer2:");
System.out.print(c2.toString());
}
}
錯誤簡短描述
測驗樣例包含兩個電腦,自然含有兩套資料
運行程式時,分開復制黏貼(也就是一套資料一套資料地輸入)可以出現預計結果;
但是,如果是兩套資料一起復制黏貼(也就是十行資料一起粘)會繼續等待輸入,而繼續輸入后會拋出java.util.InputMismatchException(輸入不匹配例外)
分析程序
首先的首先把范圍鎖定在構造方法里的scanner上;
首先猜測輸入終止條件,即換行符、空格等等;
再猜測是否有中文全角或者網頁復制連帶格式字符干擾;
都否決后,開始分析代碼邏輯,
就有了以下猜測:
一組有效資料對應一組期待輸入,可以得到一組有效輸出;
但兩組有效資料合并對應兩組期待輸入,只能得到一組期望輸出;
現在需要證明猜測,即讓錯誤顯式地表達出來,
方法:new一個物件,輸出一次這個物件的info;再new另一個物件,輸出另一個的info
錯誤簡單分析
我歸納了一下,錯誤的表現為:
一組有效資料對應一組期待輸入,可以得到一組有效輸出;
但兩組有效資料合并對應兩組期待輸入,只能得到一組期望輸出;
可能的錯誤及錯誤原因:
第一:輸入資料時,對于inputstream類或者scanner類,其中一個或兩個類的實作應該是一次性的讀入,不管用不用的上,用不上的資料就丟了,這就解釋了當第二個期待輸入時,程式得不到有效輸入,因為已經被丟了,
第二:由于今天白天討論的scanner問題,把scanner實體化在構造方法里并不合適,因為構造方法會被呼叫很多次,所以解決方法是,把scanner作為引數匯入構造方法使用,這樣就保證了scanner的唯一性,
新的博客已寫完,包含對以上問題的完全剖析
以上的分析現在看來簡直了…
還請讀者移步至新的博客中查看錯誤原因:博客鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/286744.html
標籤:其他
