我真的不知道出了什么问题,因为total被初始化了,因为程序总是以案例1开始,因为最初的点击等于1。我该在哪里初始化它,还是出了什么问题? >
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tkmsoft.taahel.activities.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="0dp"
android:layout_height="?android:actionBarSize"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.Toolbar
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:id="@+id/toolbar_title"
style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/toolbar_filter_button"
android:gravity="center|start"
android:textColor="@android:color/white" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/main_frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:4)
您需要输入外部总开关的定义,如下所示:
enterBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double total = 0;
switch(click)
{
case 1:
double subtotal = Double.parseDouble(Atext.getText());
total = 0.05*Math.round(1.13*subtotal/0.05);
answer.setText("The total will be $" + total + " (tax included). Enter the amount of money the customer gave.");
int click = 2;
case 2:
double money = Double.parseDouble(Atext.getText());
click = 3;
if (money<total)
{
JOptionPane.showMessageDialog(window, "That is not enough money.");
click = 2;
}
}
}
});
答案 1 :(得分:1)
您应该在switch语句之前将“总计”初始化为0。
答案 2 :(得分:0)
每个case
中变量的范围对应于整个switch语句
switch(key) {
case 0:
String str="zero";
break;
case 1:
str="One";
break;
}
但是如果您创建带有括号的case
,则需要在每个case
或switch
语句上方声明它
switch(key) {
case 0:{
String str="zero";
break;
}
case 1:{
String str="One";
break;
}
}
OR
String str;
switch(key) {
case 0:{
str="zero";
break;
}
case 1:{
str="One";
break;
}
}