欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

进制转换

程序员文章站 2022-07-13 13:17:19
...

很是奇怪?为什么没有continue就显示PE?

import java.util.*;
public class Main {
   public static void main(String args[]){
	   Scanner input=new Scanner(System.in);
	 
	    char a[]=new char[1001];
	   int top;
	   int m,i;
	   while(input.hasNext()){
		   top=-1;
		   int n=input.nextInt();
		   int r=input.nextInt();
		   if(n==0) {
			   
			   System.out.println("0");
			   continue;//没有continue就PE(Presention Error)???
		   }
		   else if(n<0) {
			   n=-n;
			   System.out.print("-");
		   }
		   while(n!=0){
			m=n%r;
			if(m<10) a[++top]=(char)(m+'0');//******
			else a[++top]=(char)('A'+m-10);//******
			  n/=r;
		   }
		   for(i=top;i>=0;i--)
			   System.out.print(a[i]);
		   System.out.println();
		   
	   }
   }
}

没有continue的情况(3个if)

import java.util.*;

public class Main {
	public static void main(String args[]) {
		Scanner input = new Scanner(System.in);
		// int a[]=new int[1001];
		char a[] = new char[1001];
		int top;
		int m, i;
		while (input.hasNext()) {
			top = -1;
			int n = input.nextInt();
			int r = input.nextInt();
			if (n == 0) 

				System.out.println("0");
				// continue;//没有continue就PE(Presention Error)???
			
			if (n < 0) {
				n = -n;
				System.out.print("-");
			}
			if (n > 0) {

				while (n != 0) {
					m = n % r;
					if (m < 10)
						a[++top] = (char) (m + '0');// ******
					else
						a[++top] = (char) ('A' + m - 10);// ******
					n /= r;
				}
				for (i = top; i >= 0; i--)
					System.out.print(a[i]);
				System.out.println();
			}
		}
	}
}