| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 632 人关注过本帖
标题:问题程序,请求指点!
取消只看楼主 加入收藏
victory623
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-12-8
收藏
 问题点数:0 回复次数:0 
问题程序,请求指点!
import *;
import java.util.Scanner;
public class sy2_2 {
    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Tool tool;
        tool=new Tool("剪刀",2000);
        tool.print();
        tool.set();
        tool.print();
        House house;
        house=new House("普通房子",20,10,3);
        house.print();
        house.set();
        house.print();
    }

}

class Tool{
    String name;
    int date;
    Tool(String name,int data){
        this.name=name;
        this.date=data;
        }
    void print(){
        System.out.println("名称:"+name);
        System.out.println("制造年份:"+date);
        }
    void set(){
        int choice;
        Scanner read=new Scanner(System.in);
        System.out.print("请选择要设置的属性(1-名称/2-制造年份):");
        choice=new Scanner(System.in).nextInt();
        if(choice==1){
            System.out.print("名称:");
            read.next(name);
            }
        else{
            System.out.print("制造年份:");
            read.nextInt(date);
            }
        }
    }

class House{
    String name;
    int length,width,high;
    House(String name,int length,int width,int high){
        this.name=name;
        this.length=length;
        this.width=width;
        this.high=high;
    }
    void print(){
        System.out.println("名称:"+name);
        System.out.println("长度:"+length);
        System.out.println("宽度:"+width);
        System.out.println("高度:"+high);
    }
    void set() throws IOException {
        char choice;
        Scanner read=new Scanner(System.in);
        System.out.println("请选择要设置的属性(n-名称/l-长度/w-宽度/h-高度):");
        choice=(char)System.in.read();
        switch(choice){
        case 'n':{System.out.print("名称:");read.next(name);break;}
        case 'l':{System.out.print("长度:");read.nextInt(length);break;}
        case 'w':{System.out.print("宽度:");read.nextInt(width);break;}
        case 'h':{System.out.print("高度:");read.nextInt(high);break;}
        default:System.out.println("输入错误!");
        }
    }
}

运行之后显示以下内容:
名称:剪刀
制造年份:2000
请选择要设置的属性(1-名称/2-制造年份):1
名称:锤子
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at Tool.set(sy2_2.java:41)
    at sy2_2.main(sy2_2.java:12)
我是新手,所以不太看懂,还请各位能人出手相助,不胜感激!
搜索更多相关主题的帖子: 请求 
2008-12-15 10:31
快速回复:问题程序,请求指点!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023149 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved