重複の可能性:
10 進数変換エラー
クラスのプログラムを書いていますが、8 進数を 10 進数に変換する方法がわかりません。これが私がこれまでやろうとしてきたことです:
import java.util.Scanner;
public class test
{
public static void main ( String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter number: ");
int oct = input.nextInt();
int d2count = 0;
int result=0;
int d3count = 0;
int d3 = 0;
int d2 = 0;
d3 = oct;
d2 = oct;
if(oct < 1000){
while ( d3 >= 100){
d3 = d3 - 100;
d3count++;
}}
if (oct < 100){
while ( d2 >= 10){
d2 = d2 - 10;
d2count++;
}}
result = (d3count * 64)+(d2count * 8) + d2;
System.out.printf("%d\n",result);
}
}
したがって、基本的には、数値を 1 桁に減らす方法が必要です (つまり、1337 を 1,3,3,7 に)。本当は今あるものでやりたいのですが、私のやり方には見えないエラーがいくつかあるようです。100 未満の数値を入力すると実際に機能しますが、100 を超える数値を入力すると、変換がどこかでめちゃくちゃになります。私はJavaが初めてなので、基本的なテクニックがあればあるほど良いです、ありがとう