2017年3月23日 星期四

Blogger : 文章撰寫模式切換,程式碼就會產生異常的解決方法

在Blogger上撰寫程式碼多少可能會發生格式跑掉或少字的情況

引起各種異狀的原因在於 Blogger 太過聰明(或自作聰明),自動對內容進行解析,但又解析錯誤。

解決的方法就是把程式碼轉換成HTML網頁格式

把你的程式碼,透過下面網頁進行轉碼就可以解決問題 :


沒轉碼過的程式碼 , 可以看到if判斷式後面開始出錯


public class Main3Activity extends AppCompatActivity {

    private TextView txtheight,txtweight,txtresult;
    private EditText sex,waistline;
    private Button btncalculate;
    private double height,weight;
    private int intwaistline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        findViews();
    }
    private void findViews(){
        txtheight = (TextView)findViewById(R.id.txtheight);
        txtweight = (TextView)findViewById(R.id.txtweight);
        txtresult = (TextView)findViewById(R.id.result);
        sex = (EditText)findViewById(R.id.sex);
        waistline = (EditText)findViewById(R.id.waistline);

        btncalculate = (Button)findViewById(R.id.btncalculate);
        Bundle bundle = getIntent().getExtras();
        height = bundle.getDouble("height");
        String shieght = String.valueOf(height);
        weight = bundle.getDouble("weight");
        String sweight = String.valueOf(weight);
        txtheight.setText(shieght);
        txtweight.setText(sweight);
        btncalculate.setOnClickListener(btncalculateListner);
    }
    private Button.OnClickListener btncalculateListner = new Button.OnClickListener(){
      public void onClick(View view){
          intwaistline = Integer.parseInt(waistline.getText().toString());
          String ssex = sex.getText().toString();
          switch (ssex){
              case "F":
                  if(intwaistline<80 break="" case="" code="" default:="" else="" if="" intwaistline="" txtresult.settext="">


轉換格式後就正常了 :

public class Main3Activity extends AppCompatActivity {

    private TextView txtheight,txtweight,txtresult;
    private EditText sex,waistline;
    private Button btncalculate;
    private double height,weight;
    private int intwaistline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        findViews();
    }
    private void findViews(){
        txtheight = (TextView)findViewById(R.id.txtheight);
        txtweight = (TextView)findViewById(R.id.txtweight);
        txtresult = (TextView)findViewById(R.id.result);
        sex = (EditText)findViewById(R.id.sex);
        waistline = (EditText)findViewById(R.id.waistline);

        btncalculate = (Button)findViewById(R.id.btncalculate);
        Bundle bundle = getIntent().getExtras();
        height = bundle.getDouble("height");
        String shieght = String.valueOf(height);
        weight = bundle.getDouble("weight");
        String sweight = String.valueOf(weight);
        txtheight.setText(shieght);
        txtweight.setText(sweight);
        btncalculate.setOnClickListener(btncalculateListner);
    }
    private Button.OnClickListener btncalculateListner = new Button.OnClickListener(){
      public void onClick(View view){
          intwaistline = Integer.parseInt(waistline.getText().toString());
          String ssex = sex.getText().toString();
          switch (ssex){
              case "F":
                  if(intwaistline<80)
                      txtresult.setText("正常體位");
                  else
                      txtresult.setText("體位異常");
                  break;
              case "M":
                  if(intwaistline<90)
                      txtresult.setText("正常體位");
                  else
                      txtresult.setText("體位異常");
                  break;
              default:
                  txtresult.setText("輸入錯誤");
          }

      }
    };

}


另外如果是XML的格式,可以使用下面的網址轉換:
http://coderstoolbox.net/string/#!encoding=xml&action=encode&charset=us_ascii

沒有留言:

張貼留言