Home  >  Article  >  Java  >  How to solve Java hexadecimal conversion, input, and naming problems

How to solve Java hexadecimal conversion, input, and naming problems

WBOY
WBOYforward
2023-05-12 10:46:131037browse

Convert from decimal to binary: divide by two and take the remainder until the quotient is zero. Reverse the order of the remainder and get the value represented by binary. (The same applies to octal and hexadecimal)

Binary to octal: Binary values ​​are calculated in groups of three digits.

Binary to hexadecimal: Binary four-digit calculation value.

Converting octal and hexadecimal to binary is the inverse operation of converting binary to octal and hexadecimal.

Java input seems to be more complicated than C/C (?)

First, introduce the Scanner under the package util, create an object of the Scanner, and then the input can be performed.

The code is as follows:

import java.util.Scanner;
public class cin{
  public static void main(String[] args){
    Scanner myScanner = new Scanner(System.in);//创建Scanner的对象
    System.out.print("name:");
    String name = myScanner.next();//录入姓名
    System.out.print("age:");
    int age = myScanner.nextInt();//录入年龄
    System.out.print("score:");
    double score = myScanner.nextDouble();//录入成绩
    System.out.println();
    //依次输出姓名、年龄、成绩
    System.out.print("name: " + name);
    System.out.println();
    System.outprint("age: " + age);
    System.out.println();
    System.out.print("score: " + score);
    System.out.println();
    }
  }

The result is as follows:

How to solve Java hexadecimal conversion, input, and naming problems

A knowledge point before the correction here:

Java does not automatically wrap the output

System.out.println(); will wrap

System.out.print(); will not wrap

There are also many rules for variable naming, most of which are similar to C/C. C/C identifiers can be composed of numbers, letters and underscores and can only start with a letter underscore. In Java, you can also use one (it will change when you type it, as shown in the screenshot )

How to solve Java hexadecimal conversion, input, and naming problems

(Picture from teacher Han Shunping)

At the same time, the teacher also gave the naming convention, so I won’t list them one by one here:

How to solve Java hexadecimal conversion, input, and naming problems

Finally add some knowledge points that were not mentioned before:

There are signed numbers and unsigned numbers in C/C, but there are no unsigned numbers in Java

In the Java operator

 0001, and the operation is 0100 (this is the actual use) The operation here is 4

>> means right shift: if the number is positive, the high bit is not 0; if it is negative, the high bit Complement 1

0100, after the operation is 0001 (in actual use, the operation here is 4 >> 2, but the operation is converted into two's complement operation)

>>> ; Represents unsigned right shift (logical right shift): regardless of positive or negative, the high bit is filled with 0

after the right shift. Note: There is no

#. All of the above three are Operations based on two’s complement numbers

#

The above is the detailed content of How to solve Java hexadecimal conversion, input, and naming problems. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete