Java将double转换为int
我有这个代码,文本字段中的输入是50,所以它的输出应该是25,Java将double转换为int,java,Java,我有这个代码,文本字段中的输入是50,所以它的输出应该是25, 但是有一个错误,因为没有输出 button_1.addActionListener(new ActionListener() { String tfg = textField.getText().toString(); String tfc = textField.getText(); public void actionPerformed(ActionEvent e) { if(textF
但是有一个错误,因为没有输出
button_1.addActionListener(new ActionListener() {
String tfg = textField.getText().toString();
String tfc = textField.getText();
public void actionPerformed(ActionEvent e) {
if(textField.getText().toString().equals("0") ||
textField.getText().toString().equals("") ||
textField.getText().toString().equals(" ") ||
textField.getText().toString().matches("[a-zA-Z]+")) {
JOptionPane.showMessageDialog(Cow.this,
"Tree Amount Should Be 1 or More",
"Invalid Tree Amount",
JOptionPane.ERROR_MESSAGE);
}
if(!tfg.equals("0")) {
try {
int tfgg = Integer.parseInt(tfc);
int trcs = tfgg * 1;
double trcp = 1 / 2;
int trcc = (int) trcp * trcs;
System.out.println(new Integer(trcc).toString());
} catch (NumberFormatException se) {
}
}
}
});
我尝试将我的php转换为该代码,以下是我的php代码:
$doorSeed = $mount * 1;
$doorPrice = 1 / 2;
$doorConvert = $doorPrice * $doorSeed;
echo "Treasure Chest Seed = ".$doorSeed." Price : ".$doorConvert." World Lock <br>";
$doorSeed=$mount*1;
$doorPrice=1/2;
$doorConvert=$doorPrice*$doorSeed;
回声“宝箱种子=”.$doorSeed.“价格:”.$doorConvert.“世界锁”
“;
试试这个
int trcc = (int) (trcp * trcs);
您需要将完整表达式
[(trcp*trcs)
]转换为int
,而不仅仅是第一个变量trcp
。表达式trcs
中的另一个变量为double
类型,因此表达式的结果变为double
。这就是为什么你投下了完整的表达。因此,您的最终结果将是int
int tfgg = Integer.parseInt(tfc.trim()); // tfc = 4; tfgg = 4;
int trcs = tfgg * 1; // trcs = 4 * 1 = 5;
double trcp = 1.0 / 2; // trcp = 0.5;
int trcc = (int) (trcp * trcs); // trcc = (0.5 * 5) = (2.5) = 2;
System.out.println(trcc); // 2;
double trcp = 1.0 / 2;
这里至少将一个值设置为double
,这样表达式将按照@tunaki的建议计算为double
另外,在代码inttrcs=tfgg*1中不需要此语句代码>
像这样使用:
String tfg ;
String tfc ;
public void actionPerformed(ActionEvent e) {
tfg = textField.getText();
tfc = textField.getText();
if(textField.getText().equals("0") || textField.getText().equals("") || textField.getText().equals(" ") || textField.getText().matches("[a-zA-Z]+")) {
JOptionPane.showMessageDialog(Cow.this,
"Tree Amount Should Be 1 or More",
"Invalid Tree Amount",
JOptionPane.ERROR_MESSAGE);
}
if(!tfg.equals("0")) {
try {
int tfgg = Integer.parseInt(tfc.trim()); // tfc = 4;
int trcs = tfgg; // trcs = 4 * 1 = 5
double trcp = 1.0 / 2; // trcp = 0.5
int trcc = (int) (trcp * trcs); // trcc = (0.5 * 5) = (2.5) = 2
System.out.println("HI: "+trcc);
} catch (NumberFormatException ignored) {
}
试试这个
int trcc = (int) (trcp * trcs);
您需要将完整表达式
[(trcp*trcs)
]转换为int
,而不仅仅是第一个变量trcp
。表达式trcs
中的另一个变量为double
类型,因此表达式的结果变为double
。这就是为什么你投下了完整的表达。因此,您的最终结果将是int
int tfgg = Integer.parseInt(tfc.trim()); // tfc = 4; tfgg = 4;
int trcs = tfgg * 1; // trcs = 4 * 1 = 5;
double trcp = 1.0 / 2; // trcp = 0.5;
int trcc = (int) (trcp * trcs); // trcc = (0.5 * 5) = (2.5) = 2;
System.out.println(trcc); // 2;
double trcp = 1.0 / 2;
这里至少将一个值设置为double
,这样表达式将按照@tunaki的建议计算为double
另外,在代码inttrcs=tfgg*1中不需要此语句代码>
像这样使用:
String tfg ;
String tfc ;
public void actionPerformed(ActionEvent e) {
tfg = textField.getText();
tfc = textField.getText();
if(textField.getText().equals("0") || textField.getText().equals("") || textField.getText().equals(" ") || textField.getText().matches("[a-zA-Z]+")) {
JOptionPane.showMessageDialog(Cow.this,
"Tree Amount Should Be 1 or More",
"Invalid Tree Amount",
JOptionPane.ERROR_MESSAGE);
}
if(!tfg.equals("0")) {
try {
int tfgg = Integer.parseInt(tfc.trim()); // tfc = 4;
int trcs = tfgg; // trcs = 4 * 1 = 5
double trcp = 1.0 / 2; // trcp = 0.5
int trcc = (int) (trcp * trcs); // trcc = (0.5 * 5) = (2.5) = 2
System.out.println("HI: "+trcc);
} catch (NumberFormatException ignored) {
}
试试这个
int trcc = (int) (trcp * trcs);
您需要将完整表达式
[(trcp*trcs)
]转换为int
,而不仅仅是第一个变量trcp
。表达式trcs
中的另一个变量为double
类型,因此表达式的结果变为double
。这就是为什么你投下了完整的表达。因此,您的最终结果将是int
int tfgg = Integer.parseInt(tfc.trim()); // tfc = 4; tfgg = 4;
int trcs = tfgg * 1; // trcs = 4 * 1 = 5;
double trcp = 1.0 / 2; // trcp = 0.5;
int trcc = (int) (trcp * trcs); // trcc = (0.5 * 5) = (2.5) = 2;
System.out.println(trcc); // 2;
double trcp = 1.0 / 2;
这里至少将一个值设置为double
,这样表达式将按照@tunaki的建议计算为double
另外,在代码inttrcs=tfgg*1中不需要此语句代码>
像这样使用:
String tfg ;
String tfc ;
public void actionPerformed(ActionEvent e) {
tfg = textField.getText();
tfc = textField.getText();
if(textField.getText().equals("0") || textField.getText().equals("") || textField.getText().equals(" ") || textField.getText().matches("[a-zA-Z]+")) {
JOptionPane.showMessageDialog(Cow.this,
"Tree Amount Should Be 1 or More",
"Invalid Tree Amount",
JOptionPane.ERROR_MESSAGE);
}
if(!tfg.equals("0")) {
try {
int tfgg = Integer.parseInt(tfc.trim()); // tfc = 4;
int trcs = tfgg; // trcs = 4 * 1 = 5
double trcp = 1.0 / 2; // trcp = 0.5
int trcc = (int) (trcp * trcs); // trcc = (0.5 * 5) = (2.5) = 2
System.out.println("HI: "+trcc);
} catch (NumberFormatException ignored) {
}
试试这个
int trcc = (int) (trcp * trcs);
您需要将完整表达式
[(trcp*trcs)
]转换为int
,而不仅仅是第一个变量trcp
。表达式trcs
中的另一个变量为double
类型,因此表达式的结果变为double
。这就是为什么你投下了完整的表达。因此,您的最终结果将是int
int tfgg = Integer.parseInt(tfc.trim()); // tfc = 4; tfgg = 4;
int trcs = tfgg * 1; // trcs = 4 * 1 = 5;
double trcp = 1.0 / 2; // trcp = 0.5;
int trcc = (int) (trcp * trcs); // trcc = (0.5 * 5) = (2.5) = 2;
System.out.println(trcc); // 2;
double trcp = 1.0 / 2;
这里至少将一个值设置为double
,这样表达式将按照@tunaki的建议计算为double
另外,在代码inttrcs=tfgg*1中不需要此语句代码>
像这样使用:
String tfg ;
String tfc ;
public void actionPerformed(ActionEvent e) {
tfg = textField.getText();
tfc = textField.getText();
if(textField.getText().equals("0") || textField.getText().equals("") || textField.getText().equals(" ") || textField.getText().matches("[a-zA-Z]+")) {
JOptionPane.showMessageDialog(Cow.this,
"Tree Amount Should Be 1 or More",
"Invalid Tree Amount",
JOptionPane.ERROR_MESSAGE);
}
if(!tfg.equals("0")) {
try {
int tfgg = Integer.parseInt(tfc.trim()); // tfc = 4;
int trcs = tfgg; // trcs = 4 * 1 = 5
double trcp = 1.0 / 2; // trcp = 0.5
int trcc = (int) (trcp * trcs); // trcc = (0.5 * 5) = (2.5) = 2
System.out.println("HI: "+trcc);
} catch (NumberFormatException ignored) {
}
您的问题在这一行:
double trcp = 1 / 2;
这不会导致trcp=0.5
,而是trcp=0.0
,因为您正在除以整数值(因此使用整数除法)
您应该使用以下代码:
double trcp = 1.0 / 2;
用双打迫使除法
其他意见:
newinteger(trcc).toString()
应替换为String.valueOf(trcc)
- 避免使用空的catch语句:至少记录异常
- 这一行的要点是什么:
inttrcs=tfgg*1代码>
您的问题在这一行:
double trcp = 1 / 2;
这不会导致trcp=0.5
,而是trcp=0.0
,因为您正在除以整数值(因此使用整数除法)
您应该使用以下代码:
double trcp = 1.0 / 2;
用双打迫使除法
其他意见:
newinteger(trcc).toString()
应替换为String.valueOf(trcc)
- 避免使用空的catch语句:至少记录异常
- 这一行的要点是什么:
inttrcs=tfgg*1代码>
您的问题在这一行:
double trcp = 1 / 2;
这不会导致trcp=0.5
,而是trcp=0.0
,因为您正在除以整数值(因此使用整数除法)
您应该使用以下代码:
double trcp = 1.0 / 2;
用双打迫使除法
其他意见:
newinteger(trcc).toString()
应替换为String.valueOf(trcc)
- 避免使用空的catch语句:至少记录异常
- 这一行的要点是什么:
inttrcs=tfgg*1代码>
您的问题在这一行:
double trcp = 1 / 2;
这不会导致trcp=0.5
,而是trcp=0.0
,因为您正在除以整数值(因此使用整数除法)
您应该使用以下代码:
double trcp = 1.0 / 2;
用双打迫使除法
其他意见:
newinteger(trcc).toString()
应替换为String.valueOf(trcc)
- 避免使用空的catch语句:至少记录异常
- 这一行的要点是什么:
inttrcs=tfgg*1代码>
基本上你有这个
(int)0.5=0
。
在您的例子中,trcp=0.5
是双精度的,因此当它被转换为整数时,结果是0
当您执行(int)a+b
时,此处带括号的优先级的真正操作是:((int)a)+(b)
,因此您必须执行以下操作:
int trcc = (int) (trcp * trcs);
int trcc = (int) trcp * trcs;
而不是以下内容:
int trcc = (int) (trcp * trcs);
int trcc = (int) trcp * trcs;
基本上你有这个(int)0.5=0
。
在您的例子中,trcp=0.5
是双精度的,所以当它被转换为整数时