2013-08-22 89 views
0

在某种情况下是否有办法从字符串中删除变量? 例如:有两行,每行有100,200和300垂直。 如果有人选择100,我怎么能得到它来删除100,但离开200和300 ..?在某些条件下删除变量

我还没有尝试过任何东西,但我已经把100,200等等作为变量,只是在特定的样式中打印出变量以使其看起来垂直。这些变量也是int ..

P.s这是一个危险的游戏。

+3

使用'名单',而不是一个单一的'String'来处理,那么,你的数据。 –

+0

第一个问题。是。这是计算机科学,你可以做任何事情! – SchautDollar

+0

@LuiggiMendoza你是什么意思?你能详细谈谈吗?我拥有system.out.println的所有数字......并且配置为使它看起来像是垂直的。 – jason

回答

-1

这个答案假定你有一个你想要打印的字符串,并且改变这些内容的片断。

用空格替换变量。

首先找到你想要消除串的正确位置,使用

indexOf(String str, int fromIndex), 

的indexOf( “100”,X);

对于x,您将列的索引放置在要从中消除的列。 然后提取子串从开始到想要消除的子串,

substring(int beginIndex,int endIndex);

,并用其替换成原:

replace(CharSequence target+"100", CharSequence replacement+" "); 

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

0

阅读你的问题,看来你心里有这样的事情:

int c1_100=100, c1_200=200, c1_300=300, c2_100=100, c2_200=200, c3_300=300; 
System.out.println(c1_100+"/t"+c2_100+"/t"+c1_200+"/n"+c2_200+"/t"+c1_300+"/t"+c2_300+"/t"+); 

如果你想保留这个结构,你可以用字符串代替:

String c1_100="100", c1_200="200", c1_300="300", c2_100="100", c2_200="200", c3_300="300"; 

,当玩家选择,例如问题c2_200,你可以做

c2_100=" "; 

但这不是来组织代码的最佳方式。 如果你想在一个类似表格的形式打印出来的数据,你可以使用2维数组:

int questions[][]={{100, 200, 300}, {100, 200, 300}, {100, 200, 300}}; 

,然后打印出来在一个循环:

for(int i=0, i<3; i++){ 
    for(int k=0; k<3; k++){ 
     if(question[k][i]>0){ //test if you assigned 0 to the chosen question 
     System.out.print(question[k][i]+"/t"); 
     } 
     System.out.println("/n"); 
    } 
} 

而且每次一用户选择一个问题,在其中放入0。举例来说,如果他选择在第2列和值100的问题,不要

questions[1][0]=0; 

一个更好的解决办法是不要硬编码值,但使用数组中的位置,以此来了解值:

boolean questions[][]; 
    questions=new boolean[5][3]; //here I created 5 columns and 3 rows 
    //initialize 
    for(int i=0; i<questions.length; i++){ 
      for(int k=0; k<questions[0].length; k++){ 
       questions[i][k]=true; 
      } 
    } 

    //print 
     for(int i=0; i<questions[0].length; i++){ 
       for(int k=0; k<questions.length; k++){ 
        if(questions[k][i]){ //test if you assigned true to the chosen question 
        System.out.print(100*(i+1)+"\t"); 
        } 
        else{ 
         System.out.print(" "+"\t"); 
        } 
       } 
       System.out.println(); 
     } 

和关闭过程当选择了一个问题:

questions[x][y]=false; 

输出:

100 100 100 100 100 
200 200 200 200 200 
300 300 300 300 300 

并经过

questions[1][1]=false; 

100 100 100 100 100 
200  200 200 200 
300 300 300 300 300