如何在Java中使用Array和分支?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在Java中使用Array和分支?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2204字,纯文字阅读大概需要4分钟。
内容图文
![如何在Java中使用Array和分支?](/upload/InfoBanner/zyjiaocheng/794/8558b86f938e4042a8fb6421d7e6460a.jpg)
我有diskon的问题.
if (totkes > 100000)
diskon = 0.1;
else
diskon = 0.05
我输入了totkes> 100000但这个程序显示diskon是0.05,它应该是0.1.怎么解决?我真的需要你的帮助.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] nama = new String[100];
int[] harga = new int[100];
int[] item = new int [100];
String day=" ";
int jml;
int hari;
String ptg = "";
double diskon = 0;
double total=0;
double totkes =0;
double totbay = 0;
System.out.println("1. Senin - Jumat");
System.out.println("2. Sabtu");
System.out.println("3. Minggu");
System.out.print("Masukan Pilihan : ");
hari = input.nextInt();
if (hari==1) {
day= "Senin - Jumat";
if (totkes > 100000){
diskon = 0.1;
ptg = "10%";
} else {
diskon = 0.05;
ptg = "5%";
}
} else if (hari==2){
day= "Sabtu";
if (totkes>100000){
diskon = 0.15;
ptg = "15%";
} else {
diskon = 0.1;
ptg = "10%";
}
}else if (hari == 3){
day= "Minggu";
if (totkes >100000){
diskon = 0.2;
ptg = "20%";
} else {
diskon = 0.15;
ptg = "15%";
}
}else {
System.out.print("Maaf, inputan anda salah!");
}
System.out.print("Masukan Jumlah Barang Yang Diinputkan : ");
jml = input.nextInt();
for (int i=0; i<jml; i++) {
System.out.println("Data ke-"+(i+1));
System.out.print("Nama Barang : ");
nama[i] = input.next();
System.out.print("Harga : ");
harga[i] = input.nextInt();
System.out.print("Jumlah Barang : ");
item[i] = input.nextInt();
}
System.out.println("| No | Nama Barang | Harga | Jumlah | Total |");
for (int i=0; i<jml; i++) {
System.out.print(" "+(i+1));
System.out.print(" "+nama[i]);
System.out.print(" "+harga[i]);
System.out.print(" "+item[i]);
// System.out.print(" "+ptg);
System.out.println(" "+(total = harga[i]*item[i]));
totkes=totkes+total;
totbay=totkes - (totkes*diskon);
}
System.out.println(" Hari: "+day);
System.out.println("Total Keseluruhan: "+totkes);
System.out.println("Diskon: "+diskon);
System.out.println("Diskon: "+ptg);
System.out.println("Total Bayar : "+totbay);
}
}
解决方法:
你必须在循环外进行计算.
System.out.println("Hari : "+day);
System.out.println("Total : Rp."+totkes);
System.out.println("Diskon : Rp."+ptgn);
ptg = totkes * diskon;
System.out.println("Total Bayar : Rp."+(totbay = totkes-ptg));
内容总结
以上是互联网集市为您收集整理的如何在Java中使用Array和分支?全部内容,希望文章能够帮你解决如何在Java中使用Array和分支?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。