package in;import java.util.Scanner;public class SellGoodsSystem {
static String[] goodsArray;
public void goodsDisplay(){
goodsArray=new String[]{"0:醒目","1:鲜橙多","2:潘石榴","3:娃哈哈" ,
"4:绿茶","5:红茶","6:可口可乐"};
System.out.print("商品");
for(int i=0;i<goodsArray.length;i++){
System.out.print(" "+goodsArray[i]);
}
}
public static void main(String[] args) {
SellGoodsSystem goods=new SellGoodsSystem();
goods.goodsDisplay();
System.out.print("\n");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你想要饮料的序号:");
if(scanner.hasNextInt()){
int a=scanner.nextInt();
if((a<0)&&(a>6)){
System.out.println(goodsArray[a]);
}else{
System.out.print("警告!!你的操作不正确!");
}
}
else{
System.out.println("您的操作不正确!");
}
}
}
static String[] goodsArray;
public void goodsDisplay(){
goodsArray=new String[]{"0:醒目","1:鲜橙多","2:潘石榴","3:娃哈哈" ,
"4:绿茶","5:红茶","6:可口可乐"};
System.out.print("商品");
for(int i=0;i<goodsArray.length;i++){
System.out.print(" "+goodsArray[i]);
}
}
public static void main(String[] args) {
SellGoodsSystem goods=new SellGoodsSystem();
goods.goodsDisplay();
System.out.print("\n");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你想要饮料的序号:");
if(scanner.hasNextInt()){
int a=scanner.nextInt();
if((a<0)&&(a>6)){
System.out.println(goodsArray[a]);
}else{
System.out.print("警告!!你的操作不正确!");
}
}
else{
System.out.println("您的操作不正确!");
}
}
}
解决方案 »
- 问题?如何从数组中删除1个数据?
- replace的用法
- 高分求助如何将这段 c 代码转化为java (涉及到DES加密)
- Linux下怎样用java访问windows的共享文件?
- HELP......
- 抛出和捕获
- 请大家讨论以下好的java 开发swing/awt工具,最好的免费的.
- 在JB7中默认的情况下会为程序生成一个包,程序在JB7中可以正常运行,可是在JDK中却无法运行
- 请教:如何将JTable里的内容显示在tool tip中?
- ThreadPoolExecutor 对软件的性能没有任何提升?
- jtj后面还有用,除了指针还有声明方法吗????
- 各位哥哥姐姐我初学java,是不是应该在这里提问题呀?
System.out.print("警告!!你的操作不正确!");
} else {
System.out.println(goodsArray[a]);
}
goodsArray=new String[]{"0:醒目","1:鲜橙多","2:潘石榴","3:娃哈哈" ,
"4:绿茶","5:红茶","6:可口可乐"};
放到构造方法里.
goodsArray = new String[] { "0:醒目", "1:鲜橙多", "2:潘石榴", "3:娃哈哈", "4:绿茶",
"5:红茶", "6:可口可乐" };
System.out.print("商品");
for (int i = 0; i < goodsArray.length; i++) {
System.out.print(" " + goodsArray[i]);
}
} public static void main(String[] args) {
SellGoodsSystem goods = new SellGoodsSystem();
goods.goodsDisplay();
System.out.print("\n");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你想要饮料的序号:");
if (scanner.hasNextInt()) {
int a = scanner.nextInt();
//这里判断写反了吧,太粗心了
if ((a >= 0) && (a <= 6)) {
System.out.println(goodsArray[a]);
} else {
System.out.print("警告!!你的操作不正确!");
}
} else {
System.out.println("您的操作不正确!");
}
}
}楼主要细心啊!
goods.goodsDisplay(); 执行的时候应该输出:
商品 0:醒目 1:鲜橙多 2:潘石榴 3:娃哈哈 4:绿茶 5:红茶 6:可口可乐
然后下边直接写出:
请输入你想要饮料的序号:
您的操作不正确!
* @param args
*/
private String[] goodsArray;
public static void main(String[] args) {
SellGoodsSys goods = new SellGoodsSys();
System.out.println();
Scanner scan = new Scanner(System.in);
System.out.print("请输入一个0~6之间的数字:");
if(scan.hasNextInt()){
int a = scan.nextInt();
if(a>=0&&a<=6){
System.out.println(goods.goodsArray[a]);
}else{
System.out.println("您的操作不正确!!!");
}
}else{
System.out.println("您输入的不是整数");
}
}
public SellGoodsSys(){
goodsArray = new String[]{"0:醒目","1:鲜橙多","2:潘石榴","3:娃哈哈","4:绿茶","5:红茶","6:可口可乐"};
System.out.print("商品:");
for(int i=0;i<goodsArray.length;i++){
System.out.print(" "+goodsArray[i]);
}
}
}
写错了
应为
if((a>=0)&&(a<=6))