所以我剛剛開始了一門IT課程,並且作爲其中的一部分,我們正在學習使用Java編寫代碼;我有一個下週的任務,雖然我已經想通了,但我只是有一個問題,爲什麼它的工作原理:P在Java中減少操作
目標是編寫一段代碼,讀取一個數字,減少它,轉動它是負面的,然後輸出它。
這是我原本:
import java.util.Scanner;
// imports the Scanner utility to Java
public class Question3 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
// defines the scanner variable and sets it to recognize inputs from the user
System.out.println("Please enter a number: ");
//prompts captures a number form the screen
int a = s.nextInt();
// defines an integer variable('a') as to be set by input from the scanner
--a;
// decrement calculation(by 1)
-a;
//inverts the value of a
System.out.println("Your number is: " + a);
// outputs a line of text and the value of a
然而,Eclipse的(我使用的IDE)將不承認一元減運算符(「 - 」),所以它沒有工作。我得到了它通過調整它寫入如下工作:這裏
import java.util.Scanner;
// imports the Scanner utility to Java
public class Question3 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
// defines the scanner variable and sets it to recognize inputs from the user
System.out.println("Please enter a number: ");
//prompts captures a number form the screen
int a = s.nextInt();
// defines an integer variable('a') as to be set by input from the scanner
--a;
// decrement calculation(by 1)
System.out.println("Your number is: " + (-a));
// outputs a line of text and the inverse of the variable 'a'
我的問題是,爲什麼會在二審中一元減工作,但不是第一?