本文介绍了如何从命令行获取双输入和JOptionPane的双输入并将它们相乘以打印结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JoptionPane和Command行上的数字请求,但我不知道如何存储它们将它们相乘,请你看一下并建议我可以使用的任何选项让它工作,这里是我目前的代码:







I have the request for the numbers on the JoptionPane and the Command line but I dont know how to store them to multiply them together, can you please have a look and suggest any options I can use to get it working, here is my current code :



import java.util.Scanner;
import javax.swing.JOptionPane;
/**
 *
 * @author Adam
 */
public class Inputting {


    public static void main(String[] args) {
        Scanner Scan = new Scanner (System.in); // Creates the Scanner to allow input



        System.out.print("Enter a Number");
        String input = null;
        String first = Scan.next();



        String Second; // Second Declared as a string
        Second = JOptionPane.showInputDialog("Enter Another Number");
        double d = Double.parseDouble(Second);

        System.out.print("What is your Name?");
        String name = Scan.next();

        String Age; // Age Declared as a String
        Age = JOptionPane.showInputDialog("Enter your age ");

        System.out.print( name + " "+ "(Aged" + " " + Age +  ")" + "," + "your answer is " + first*Second );








    }

}

推荐答案

...
String first = Scan.next();
...
Second = JOptionPane.showInputDialog("Enter Another Number");





此后你只将



After this you convert only

second

转换为数值,但不要使用它!



into a numeric value, but don't use it!

double d = Double.parseDouble(Second);





解决方案是转换



Solution is to convert

first

Second

成这样的数值:



into numeric values like this:

double firstValue = Double.parseDouble(first);
double secondValue = Double.parseDouble(Second);

// get the result
double result = firstValue * secondValue;





现在你可以打印它:





Now you can print it:

System.out.print( name + " "+ "(Aged" + " " + Age +  ")" + "," + "your answer is " + result );


这篇关于如何从命令行获取双输入和JOptionPane的双输入并将它们相乘以打印结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 11:59