重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
public class Test {
创新互联主要从事网站设计、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务奉化,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = "";
while(true) {
System.out.println("请输入一个字符:");
str = sc.nextLine();
if(str=="exit") // 如果字符串为"exit",退出循环
break;
if(str.length()==1) {
char c = str.charAt(0);
System.out.println("它对应的ASCII码为:" + (int)c); //(int)c将c强制转换为它的ascii码
}
else
System.out.println("请输入一位字符。");
}
sc.close();
}
}
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;
public class TestFrame extends javax.swing.JFrame {
private JScrollPane jScrollPane1;
private JTable jTable1;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestFrame inst = new TestFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public TestFrame() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jScrollPane1 = new JScrollPane();
getContentPane().add(jScrollPane1, BorderLayout.CENTER);
{
TableModel jTable1Model =
new DefaultTableModel(
new String[][] { {"","",""},{} ,{} ,{} },
new String[] { "上网总时间", "上网花费金额" ,"上网单价"});
jTable1 = new JTable();
jScrollPane1.setViewportView(jTable1);
jTable1.setModel(jTable1Model);
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.Scanner;
public class JieZhang {
public static void main(String[] args) {
System.out.println("请输入要结账的顾客数:");
Scanner s = new Scanner(System.in);
int num = s.nextInt();
for(int i=0;inum;i++){
int flag = 1;
double sum = 0;//总价
while(flag==1){
flag = 1;
System.out.println("请输入单价:");
double price = s.nextDouble();
System.out.println("请输入数量:");
int number = s.nextInt();
sum = sum + price * number;
System.out.println("请选择是否继续计算:1继续");
flag = s.nextInt();
}
System.out.println("第"+(i+1)+"位顾客消费:"+sum);
}
}
}
先定义商品接口,然后声明购物车这个类用来保存商品,定义怎样增加商品,定义implements是一个类实现一个商品接口用的关键字,最后定义所购买的商品,然后输出,最后统计结账。
当然用链表也可以实现
刚帮你调测了一下,当你输入"n"的时候,已经跳出循环了,只不过你没有输出提示,以为没出来,
又输入字符了所有报错了,应该是输入金额的,我帮你加了下输出你可以看下结果;
public static void main(String args[]){
System.out.println("********************");
System.out.println("");
System.out.println("请选择购买商品编号:");
System.out.println("1.T恤(¥245.0) 网球鞋(¥570.0) 网球拍(¥500.0)");
System.out.println("********************");
System.out.println("");
double s=0;
String d;
do{
System.out.println("请输入商品编号:");
Scanner input=new Scanner(System.in);
int a=input.nextInt();
if(a==1){
System.out.println("请输入购买数量:");
int b=input.nextInt();
double c=245*b;
System.out.println("T恤¥245.0 数量:"+b+" "+"合计:"+c);
s=s+c;
}
else if(a==2){
System.out.println("请输入购买数量:");
int b=input.nextInt();
double c=570*b;
System.out.println("网球鞋¥245.0 数量:"+b+" "+"合计:"+c);
s=s+c;
}
else if(a==3){
System.out.println("请输入购买数量:");
int b=input.nextInt();
double c=500*b;
System.out.println("网球拍¥245.0 数量:"+b+" "+"合计:"+c);
s=s+c;
}
else {System.out.println("无此商品");}
System.out.println("否继续(y/n)");
d=input.next();
}while(d.equals("y"));
System.out.println("===========付款===========");
System.out.println("=====输入金额=====");
Scanner input=new Scanner(System.in);
int x=input.nextInt();
double m=s*0.8;
System.out.println("折扣:0.8");
System.out.println("应付金额:"+m);
System.out.println("实际金额:"+x);
double p=x-m;
System.out.println("找钱:"+p);
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
enum Huo {
TX(1, "T恤", 245), WQX(2, "网球鞋", 570), WQP(3, "网球拍", 320);
private int id;
private String name;
private double price;
private Huo(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public static Huo getById(int id) {
for (Huo item : Huo.values()) {
if (item.id == id) {
return item;
}
}
return null;
}
}
public class Test {
private static MapInteger, Integer countMap = new HashMapInteger, Integer();
private static double sum = 0;
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("**********************************************");
System.out.println("请选择购买的商品编号:");
for (Huo item : Huo.values()) {
System.out.print(item.getId() + ":" + item.getName() + " ");
}
System.out.println();
System.out.println("**********************************************");
System.out.print("输入购买商品编号:");
int id = in.nextInt();
System.out.print("输入购买商品数量:");
int count = in.nextInt();
Integer tmp = countMap.get(id);
if (tmp == null) {
tmp = 0;
}
countMap.put(id, tmp + count);
show();
while (true) {
System.out.print("是否继续(y/n):");
String jixu = in.next();
if ("Y".equalsIgnoreCase(jixu)) {
break;
} else if ("N".equalsIgnoreCase(jixu)) {
System.out.print("请支付:");
double zhifu = in.nextDouble();
System.out.println("应付金额:" + sum);
System.out.println("客户支付:" + zhifu);
System.out.println("找钱:" + (zhifu - sum));
System.exit(0);
} else {
System.out.print("输入错误");
}
}
}
}
private static void show() {
IteratorInteger it = countMap.keySet().iterator();
while (it.hasNext()) {
Integer key = it.next();
Integer count = countMap.get(key);
Huo huo = Huo.getById(key);
double heji = huo.getPrice() * count;
System.out.println(huo.getName() + "¥" + huo.getPrice() + " 数量:" + count + " 合计:" + heji);
sum += heji;
}
}
}