我是BSIT大学一年级编程的新手,我们的任务是编写一个SimpleArithmetic,它会首先询问你的名字,然后你会被要求输入第一个和第二个整数。
在给出要求之后它必须显示“你好(输入的名字)”然后接下来的是两个整数的总和,差异,产品和模型,最后它将显示“谢谢你!”。
我尝试了很多代码,但我不会跑,所以有人能帮助我吗?我真的很感激,因为我真的很想知道会发生什么。
这是我的代码
public class SimpleArithmetic{
public static void main(String[] args){
//1: Declare name as symbol
//2: num 1, num 2, sum, difference, product, mod to 0;
System.out.print("Enter your name: ");
name = in.next(); // <---- HERE
System.out.printf("\nEnter first integer: ");
System.out.printf("\nEnter second integer: ");
System.out.printf("\nnum 1 + num 2");
System.out.printf("\nnum 1 - num 2");
System.out.printf("\nnum 1 * num 2");
System.out.printf("\nnum 1 % num 2");
System.out.print("Hello \n + name");
System.out.println("num 1" + "+" + "is" + "sum");
System.out.println("num 1" + "-" + "is" + "difference");
System.out.println("num 1" + "*" + "is" + "product");
System.out.println("num 1" + "%" + "is" + "mod");
System.out.print("Thank You!");
}
}
当我尝试编译java文件时,粗体是错误
答案 0 :(得分:1)
使用以下类扫描仪读取输入:
Scanner scanner = new Scanner(System.in); // Init scanner
String name = scanner.nextLine(); // Reads a full line
int a = scanner.nextInt(); // Reads one integer
int b = scanner.nextInt(); // Reads another integer
如果您想了解有关课程Scanner
的更多信息,请查看文档here。
基本上,在这种情况下,扫描程序是一个从流(System.in
)读取输入的有用类。来自javadoc
一个简单的文本扫描程序,可以解析基本类型和字符串 使用正则表达式。
答案 1 :(得分:0)
以下内容适用于您......
package com.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class SimpleArithmetic{
public static void main(String[] args){
try{
//1: Declare name as symbol
//2: num 1, num 2, sum, difference, product, mod to 0;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name: ");
String name = in.readLine(); // <---- HERE
System.out.printf("\nEnter first integer: ");
String str1 = in.readLine();
int number1 = Integer.parseInt(str1);
System.out.printf("\nEnter second integer: ");
String str2 = in.readLine();
int number2 = Integer.parseInt(str2);
System.out.print("Hello \n "+ name);
System.out.println("num 1" + "+" + "is" + (number1 + number2));
System.out.println("num 1" + "-" + "is" + (number1 - number2));
System.out.println("num 1" + "*" + "is" + (number1 * number2));
System.out.println("num 1" + "%" + "is" + (number1 % number2));
System.out.print("Thank You!");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
答案 2 :(得分:0)
删除变量上的所有双qoutes。
将System.out.print("Hello \n + name");
更改为
System.out.print("Hello \n + name);
System.out.println("num 1" + "+" + "is" + "sum");
至System.out.println("num 1" + "+" + "is" + sum);
System.out.println("num 1" + "-" + "is" + "difference"); to `System.out.println("num 1" + "-" + "is" + difference);`
System.out.println("num 1" + "*" + "is" + "product");
至System.out.println("num 1" + "*" + "is" + product);
System.out.println("num 1" + "%" + "is" + "mod"); to `System.out.println("num 1" + "%" + "is" + mod);`
答案 3 :(得分:0)
使用Scanner并尝试catch块:
分配注释中没有列出变量声明。
你应该做类似的事情:
import java.util.Scanner;
public class SimpleArithmetic {
int num1 = 0, num2 = 0, sum = 0, difference = 0, product = 0, mod = 0;
String name = null;
Scanner in = null;
public static void main(String[] args) {
SimpleArithmetic sa = new SimpleArithmetic();
try {
sa.doSimpleArithmetic();
} catch (Exception e) {
e.printStackTrace();
}
}
void doSimpleArithmetic() throws Exception {
in = new Scanner(System.in);
System.out.print("Enter your name: ");
name = in.nextLine();
System.out.printf("\nEnter first integer: ");
num1 = Integer.parseInt(in.nextLine());
System.out.printf("\nEnter second integer: ");
num2 = Integer.parseInt(in.nextLine());
sum = num1 + num2;
difference = num1 - num2;
product = num1 * num2;
mod = num1 / num2;
System.out.println("\n" + "Hello " + name + "\n");
System.out.println(num1 + " + " + num2 + " is :" + sum);
System.out.println(num1 + " - " + num2 + " is :" + difference);
System.out.println(num1 + " * " + num2 + " is :" + product);
System.out.println(num1 + " % " + num2 + " is :" + mod);
System.out.println("\n" + "Thank You!");
in.close();
}
}
答案 4 :(得分:0)
$ec2 = new Aws\Ec2\Ec2Client([credentials]);
$result = $ec2->runInstances(array(
'DryRun' => false,
// ImageId is required
'ImageId' => '[removed]',
// MinCount is required
'MinCount' => 1,
// MaxCount is required
'MaxCount' => 1,
'KeyName' => '[removed]',
'SecurityGroupIds' => array('[removed]'),
'InstanceType' => 'r3.4xlarge',
'Placement' => array(
'AvailabilityZone' => 'us-east-1a',
),
'EbsOptimized' => false,
'Monitoring' => array(
// Enabled is required
'Enabled' => true,
),
));
$arr = $result->toArray();
$instanceId = $arr['Instances'][0]['InstanceId'];
echo 'created instance '.$instanceId."\n";
$ec2->waitUntil('InstanceRunning', ['InstanceIds' => array($instanceId)]);
$result = $ec2->describeInstances(array(
'InstanceIds' => array($instanceId),
));
$dnsName = current($result->getPath('Reservations/*/Instances/*/PublicDnsName'));