问题描述
importjava.io.*;publicclassCalculator{publicstaticvoidmain(String[]args)throwsIOException{System.out.println("****************************************");System.out.println("**");System.out.println("*路由汇总计算器*");System.out.println("**");System.out.println("****************************************");System.out.println("");int[][]datasave=newint[10][5];intcount=0;booleanstop=false;while(count<10&&stop==false){InputStreamReaderkeyboardin=newInputStreamReader(System.in);BufferedReaderinput=newBufferedReader(keyboardin);System.out.println("请输入网络地址(格式10.0.0.0/8)个数:"+(count+1)+"最大个数:10");Stringinputdata=input.readLine();booleaninputerror=false;for(inti=0,x=0,y=0;i<inputdata.length();i++){if(inputdata.charAt(i)=='.'){x=x+1;}if(inputdata.charAt(i)=='/'){y=y+1;}if(i==inputdata.length()-1){if(x!=3&&y!=1){inputerror=true;}}}if(inputerror==false){String[]inputnetwork=inputdata.split("/",2);String[]inputoctet=inputnetwork[0].split("\.",4);int[]octet=newint[5];octet[0]=Integer.parseInt(inputoctet[0]);octet[1]=Integer.parseInt(inputoctet[1]);octet[2]=Integer.parseInt(inputoctet[2]);octet[3]=Integer.parseInt(inputoctet[3]);octet[4]=Integer.parseInt(inputnetwork[1]);if(octet[0]<255&&octet[0]>223){System.out.println("只支持ABC类IP地址.");inputerror=true;}else{for(inti=0;i<4;i++){if(octet[i]>255||octet[i]<0){inputerror=true;}}}int[]subnet=newint[]{0,0,0,0};if(octet[4]>30||octet[4]<1){inputerror=true;if(octet[4]>30&&octet[4]<=32){System.out.println("应该输入主机地址.");}}else{if(octet[4]>0&&octet[4]<=8){subnet[0]=256-((int)Math.pow(2,(8-octet[4])));}elseif(octet[4]>8&&octet[4]<=16){subnet[0]=255;subnet[1]=256-((int)Math.pow(2,(16-octet[4])));}elseif(octet[4]>16&&octet[4]<=24){subnet[0]=255;subnet[1]=255;subnet[2]=256-((int)Math.pow(2,(24-octet[4])));}elseif(octet[4]>24&&octet[4]<=30){subnet[0]=255;subnet[1]=255;subnet[2]=255;subnet[3]=256-((int)Math.pow(2,(30-octet[4])));}}if(inputerror==false){int[]hosttonetwork=newint[4];for(inti=0;i<4;i++){hosttonetwork[i]=octet[i]&subnet[i];}datasave[count][0]=hosttonetwork[0];datasave[count][1]=hosttonetwork[1];datasave[count][2]=hosttonetwork[2];datasave[count][3]=hosttonetwork[3];datasave[count][4]=octet[4];booleanishost=false;for(inti=0;i<4;i++){if(hosttonetwork[i]!=octet[i]){ishost=true;}}if(ishost){System.out.println("主机地址-->网络地址:");}System.out.println("网络地址:"+hosttonetwork[0]+"."+hosttonetwork[1]+"."+hosttonetwork[2]+"."+hosttonetwork[3]+"记录成功.");}}if(inputerror){System.out.println("IP语法错误.");count=count-1;}count=count+1;System.out.println("是否继续输入?(Y/N)");Stringmorenetwork=input.readLine();if(morenetwork.charAt(0)=='N'||morenetwork.charAt(0)=='n'){stop=true;if(count==1){System.out.println("请不要浪费CPU.");}}}if(count>1){System.out.println("");System.out.println("计算过程:");System.out.println("---------------------------------------------------------");String[][]datastring=newString[10][4];for(intx=0;x<count;x++){for(inti=0;i<4;i++){Stringtemp=Integer.toBinaryString(datasave[x][i]);Stringresult="00000000".substring(0,8-temp.length())+temp;datastring[x][i]=result;}}for(inti=0;i<count;i++){System.out.print(datastring[i][0]+"."+datastring[i][1]+"."+datastring[i][2]+"."+datastring[i][3]);System.out.println("(Network:"+datasave[i][0]+"."+datasave[i][1]+"."+datasave[i][2]+"."+datasave[i][3]+"/"+datasave[i][4]+")");}System.out.println("---------------------------------------------------------");System.out.println("");int[]diffpos=newint[]{8,8,8,8};for(inty=0;y<4;y++){for(intx=0;x<8;x++){charinitvalue=datastring[0][y].charAt(x);for(inti=1;i<count;i++){if(datastring[i][y].charAt(x)!=initvalue){diffpos[y]=x;i=count;x=8;}}}}Stringresultmask="";Stringresult="";intresultsmask=0;for(inti=0;i<4;i++){if(diffpos[i]==8){result=result+datastring[0][i];}elseif(diffpos[i]!=8){result=result+datastring[0][i].substring(0,diffpos[i]);i=4;}}resultsmask=result.length();resultmask="11111111111111111111111111111111".substring(0,result.length())+"00000000000000000000000000000000".substring(0,32-result.length());result=result+"00000000000000000000000000000000".substring(0,32-result.length());System.out.println("二进制结果:");System.out.println("---------------------------------------------------------");System.out.println(result.substring(0,8)+"."+result.substring(8,16)+"."+result.substring(16,24)+"."+result.substring(24,32)+"(网络号)");System.out.println(resultmask.substring(0,8)+"."+resultmask.substring(8,16)+"."+resultmask.substring(16,24)+"."+resultmask.substring(24,32)+"(子网掩码)");System.out.println("---------------------------------------------------------");System.out.println("");System.out.println("十进制结果:");System.out.println("---------------------------------------------------------");System.out.println(Integer.parseInt(result.substring(0,8),2)+"."+Integer.parseInt(result.substring(8,16),2)+"."+Integer.parseInt(result.substring(16,24),2)+"."+Integer.parseInt(result.substring(24,32),2)+"(网络号)");System.out.println(Integer.parseInt(resultmask.substring(0,8),2)+"."+Integer.parseInt(resultmask.substring(8,16),2)+"."+Integer.parseInt(resultmask.substring(16,24),2)+"."+Integer.parseInt(resultmask.substring(24,32),2)+"(子网掩码)");System.out.println("="+Integer.parseInt(result.substring(0,8),2)+"."+Integer.parseInt(result.substring(8,16),2)+"."+Integer.parseInt(result.substring(16,24),2)+"."+Integer.parseInt(result.substring(24,32),2)+"/"+resultsmask);System.out.println("---------------------------------------------------------");System.out.println("");charipclass='C';Stringdefaultmask="/24(255.255.255.0)";intdefaultsmask=24;if(Integer.parseInt(result.substring(0,8),2)<192){ipclass='B';defaultmask="/16(255.255.0.0)";defaultsmask=16;if(Integer.parseInt(result.substring(0,8),2)<128){ipclass='A';defaultmask="/8(255.0.0.0)";defaultsmask=8;}}System.out.println("默认地址类型"+Integer.parseInt(result.substring(0,8),2)+"."+Integer.parseInt(result.substring(8,16),2)+"."+Integer.parseInt(result.substring(16,24),2)+"."+Integer.parseInt(result.substring(24,32),2)+":"+ipclass);System.out.println("默认掩码:"+Integer.parseInt(result.substring(0,8),2)+"."+Integer.parseInt(result.substring(8,16),2)+"."+Integer.parseInt(result.substring(16,24),2)+"."+Integer.parseInt(result.substring(24,32),2)+":"+defaultmask);if(resultsmask<defaultsmask){System.out.println("Supernetting:Yes");}else{System.out.println("Supernetting:No");}System.out.println("---------------------------------------------------------");}System.out.println("");System.out.println("结束程序");}}
解决方案
解决方案二:
解决方案三:
大致看了下,好像没什么特殊的东西,Console.ReadLine和Console.WriteLine替换掉大部分后,其它代码都可以在C#中找到对应的方法或类型
解决方案四:
你不如摘出几个不懂的语句来提问,那样兴许有人管你,发这种帖子说明你是有多懒,把论坛上的人都当劳动力使唤吗?
解决方案五:
直接把这段代码粘到vs项目里面,查看那些报错的类型,去百度一下对应C#应该是什么类型。